简体   繁体   中英

How to get values from a multichoice listview alertdialog

I have an ArrayList of Person class containing age and name of the person. In the adapter (extends BaseAdapter), I have two TextViews(for setting the values of age and name) and a checkbox. This is needed to be inflated into alert dialog.

How can I implement this using multichoice of the alert dialog.

Also I did look at some examples but did not understand about the boolean[] values attribute in the alert dialog, this is the code I have so far but it still needs to implement that multichoice mode..

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

My alertdialog..

  AlertDialog.Builder ab=new AlertDialog.Builder(CustomListActivity.this);
                LayoutInflater linf=(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
                View v1=linf.inflate(R.layout.dialog_list, null);
                ab.setView(v1);
                //ab.setTitle("Select a group");

                lv=(ListView) v1.findViewById(R.id.listView1);
                 ma=new MyAdapter(CustomListActivity.this, plist);
                lv.setAdapter(ma);

And MYAdapter ..

public class MyAdapter extends BaseAdapter 
{
    Context ctx;
    ArrayList<Person> plist;
    LayoutInflater linf;
    public PersonHolder ph=null;


    public MyAdapter(Context ctx, ArrayList<Person> plist) {
        super();
        this.ctx = ctx;
        this.plist = plist;
    }

    public class PersonHolder
    {
        public TextView age;
        public TextView name;
        public CheckBox check;
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return plist.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return plist.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stub

        linf=(LayoutInflater) ctx.getSystemService(ctx.LAYOUT_INFLATER_SERVICE);
        if(convertView==null)
        {
            convertView=linf.inflate(R.layout.row_item, null);
            ph=new PersonHolder();
            ph.age=(TextView) convertView.findViewById(R.id.text1);
            ph.name=(TextView) convertView.findViewById(R.id.text2);
            ph.check=(CheckBox) convertView.findViewById(R.id.checkBox1);
            convertView.setTag(ph);
        }
        else
        {
            ph=(PersonHolder) convertView.getTag();
        }

        Person p=(Person) getItem(position);
        ph.age.setText(p.getAge());
        ph.name.setText(p.getName());
        ph.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // TODO Auto-generated method stub
//              if(ph.check.isChecked())
//              {
                    ph.check.setSelected(arg1);
//              }
//              else
//              {
//                  ph.check.setSelected(false);
//              }
            }
        });
        return convertView;
    }

}

I have created a multichoice dialog myself, this is what i am doing, basically you need to catch each click to the listview and memorize whatever it is you selected/set there.

private void startDayPickerDialog() {

    String[] days = getActivity().getResources().getStringArray(R.array.dayNames);
    days = Arrays.copyOf(days, 7);

    final Adapter a = new Adapter(getActivity());

    AlertDialog d = new AlertDialog.Builder(getActivity())
    .setTitle(R.string.repeat)
    .setAdapter(a, new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    }).setPositiveButton(R.string.done, new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    }).create();

    d.getListView().setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> lv, View view, int position, long id) {
            CheckBox cb = (CheckBox)view.findViewById(R.id.checkbox);
            cb.setChecked(!cb.isChecked());
            a.getItem(position).setChecked(!a.getItem(position).isChecked());               
        }


    });

    d.setCanceledOnTouchOutside(true);
    d.show();
}

Change your adapter to following and just call getSelected on adapter.

public class MyAdapter extends BaseAdapter 
{
    Context ctx;
    ArrayList<Person> plist;
    LayoutInflater linf;
    public PersonHolder ph=null;
    private ArrayList<Integer> selected=new ArrayList<Integer>();


    public MyAdapter(Context ctx, ArrayList<Person> plist) {
        super();
        this.ctx = ctx;
        this.plist = plist;
    }

    public class PersonHolder
    {
        public TextView age;
        public TextView name;
        public CheckBox check;
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return plist.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return plist.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stub

        linf=(LayoutInflater) ctx.getSystemService(ctx.LAYOUT_INFLATER_SERVICE);
        if(convertView==null)
        {
            convertView=linf.inflate(R.layout.row_item, null);
            ph=new PersonHolder();
            ph.age=(TextView) convertView.findViewById(R.id.text1);
            ph.name=(TextView) convertView.findViewById(R.id.text2);
            ph.check=(CheckBox) convertView.findViewById(R.id.checkBox1);
            convertView.setTag(ph);
        }
        else
        {
            ph=(PersonHolder) convertView.getTag();
        }

        Person p=(Person) getItem(position);
        ph.age.setText(p.getAge());
        ph.name.setText(p.getName());
        ph.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // TODO Auto-generated method stub
//              if(ph.check.isChecked())
//              {
                    ph.check.setSelected(arg1);
                    if(arg1){ 
                        selected.add(position);
                    }else{
                        selected.remove(position);
                    }
//              }
//              else
//              {
//                  ph.check.setSelected(false);
//              }
            }
        });
        return convertView;
    }
 public ArrayList<Integer> getSelected(){
     return selected;
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM