Android Multiple select item using button and dialog checkbox and display the selected item in listview instead of button
Asked Answered
C

2

6

i am trying to do Android Multiple select item using button and dialog checkbox, and display the selected item in listview(as in picture) instead of button(current displayed in button: please refer picture)

i am able to display the reciever in button below but i want the same to be displayed in list view:

currently displaying after selecting from dialog checkbox like:

"Receiver1, Receiver2"

I want like:

image of Receiver1 with Receiver1 Name
image of Receiver2 with Receiver2 Name

SendServiceActivity.java

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Layout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.ImageView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

public class SendServiceActivity extends AppCompatActivity implements View.OnClickListener{
protected Button selectReceiversBtn;
    protected CharSequence[] receivers = {
            "Receiver1", "Receiver2", "Receivers3"};
    protected ArrayList<CharSequence> selectedReceivers = new ArrayList<>();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 

        selectReceiversBtn = (Button) findViewById(R.id.select_receivers);
        selectReceiversBtn.setOnClickListener((View.OnClickListener) this);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()) {
            case R.id.select_receivers:
                showSelectReceiversDialog();
                break;
            default:
                break;
        }
    }   

    protected void onChangeSelectedReceivers() {
        StringBuilder stringBuilder = new StringBuilder();

        for(CharSequence receivers : selectedReceivers)
            stringBuilder.append(receivers + ",");
        selectReceiversBtn.setText(stringBuilder.toString());
    }

    protected void showSelectReceiversDialog() {
        boolean[] checkedReceivers = new boolean[receivers.length];
        int count = receivers.length;

        for(int i = 0; i < count; i++)
            checkedReceivers[i] = selectedReceivers.contains(receivers[i]);

        DialogInterface.OnMultiChoiceClickListener receiversDialogListener = new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                if(isChecked)
                    selectedReceivers.add(receivers[which]);
                else
                    selectedReceivers.remove(receivers[which]);

                onChangeSelectedReceivers();
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder
                .setTitle("Select Receivers")
                .setMultiChoiceItems(receivers, checkedReceivers, receiversDialogListener)
                .setCancelable(false)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                });
        AlertDialog dialog = builder.create();
        dialog.show();
    }
}

activity_main.xml

            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="5dip"
                android:text="Select Receivers" />

            <Button
                android:id="@+id/select_receivers"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="5dip"
                android:text="- None Selected -" />


        </LinearLayout>

enter image description here

Cleek answered 6/11, 2015 at 14:17 Comment(0)
F
4

full source code of your requirement below:

mainactivity.class

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Layout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.ImageView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

public class MainActivity extends Activity implements View.OnClickListener{
    ArrayAdapter<CharSequence> adapter;
    ListView listView1;
    protected Button selectReceiversBtn;
    protected CharSequence[] receivers = {
            "Receiver1", "Receiver2", "Receivers3"};
    protected ArrayList<CharSequence> selectedReceivers = new ArrayList<>();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 

        listView1=(ListView) findViewById(R.id.listView1);
        selectReceiversBtn = (Button) findViewById(R.id.select_receivers);
        selectReceiversBtn.setOnClickListener((View.OnClickListener) this);


       // String[] your_array = {"a","b","c"}
        adapter = new ArrayAdapter<CharSequence>(this,
                android.R.layout.simple_list_item_multiple_choice, selectedReceivers);
        listView1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        listView1.setAdapter(adapter);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()) {
            case R.id.select_receivers:
                showSelectReceiversDialog();
                break;
            default:
                break;
        }
    }   

    protected void onChangeSelectedReceivers() {
        StringBuilder stringBuilder = new StringBuilder();

        for(CharSequence receivers : selectedReceivers)
            stringBuilder.append(receivers + ",");
        adapter.notifyDataSetChanged();
        //selectReceiversBtn.setText(stringBuilder.toString());
    }

    protected void showSelectReceiversDialog() {
        boolean[] checkedReceivers = new boolean[receivers.length];
        int count = receivers.length;

        for(int i = 0; i < count; i++)
            checkedReceivers[i] = selectedReceivers.contains(receivers[i]);

        DialogInterface.OnMultiChoiceClickListener receiversDialogListener = new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                if(isChecked)
                    selectedReceivers.add(receivers[which]);
                else
                    selectedReceivers.remove(receivers[which]);

                onChangeSelectedReceivers();
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder
                .setTitle("Select Receivers")
                .setMultiChoiceItems(receivers, checkedReceivers, receiversDialogListener)
                .setCancelable(false)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();

                    }
                });
        AlertDialog dialog = builder.create();
        dialog.show();
    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dip"
        android:text="Select Receivers" />

    <Button
        android:id="@+id/select_receivers"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dip"
        android:text="- None Selected -" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

copy and paste above code..when you check in dialog it automatic add in listview

Flagelliform answered 9/11, 2015 at 9:20 Comment(0)
F
2

Below code will work for you:

String[] your_array = {"a","b","c"}
        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, your_array);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        listView.setAdapter(adapter);

Using above code, You can checked-unchecked in list view.

Flagelliform answered 7/11, 2015 at 12:23 Comment(12)
@ Ketan how I can implement your code in my code, since new in Java so asking you.Cleek
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, receivers); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); listView.setAdapter(adapter);Flagelliform
above 3 line use instend of open dialogFlagelliform
use listview in xml fileFlagelliform
you can use example from below link.. adanware.blogspot.in/2012/04/…Flagelliform
I am not looking for checkable listview, i am using checkbox as alert dialog(which i have already done), i want the selected item to be displayed in listview from checkbox. concept is: when i click on buton the alter dialog appears which has checkbox then i select some recievers and press on ok, after saying ok the selected recievers should be displayed in list view, currently its displaying on same button with comma seperated as you can see in screenshotCleek
then why not display listview on button click(ok) using arraylist(selectedReceivers)..Flagelliform
like below.. adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, selectedReceivers); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); listView.setAdapter(adapter);Flagelliform
not able to do, can you please guide the same in code?Cleek
ok..you can use below full code..just copy and pase.. only need to change main_activity.class nameFlagelliform
Thanks Ketan, It worked only thing is i don't want check box in selected list view since its already there in dialogboxCleek
Let us continue this discussion in chat.Flagelliform

© 2022 - 2024 — McMap. All rights reserved.