如何从multichoice listview alertdialog获取值

时间:2014-01-07 10:38:53

标签: java android

我有一个Person类的ArrayList,包含人的年龄和姓名。在适配器(扩展BaseAdapter)中,我有两个TextView(用于设置年龄和名称的值)和一个复选框。这需要膨胀到警报对话框中。

如何使用警报对话框的多重选项实现此功能。

此外,我确实查看了一些示例,但是没有理解警报对话框中的boolean [] values属性,这是我到目前为止的代码,但它仍然需要实现多重模式..

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

我的警报..

  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);

和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;
    }

}

2 个答案:

答案 0 :(得分:1)

我自己创建了一个多重对话框,这就是我正在做的事情,基本上你需要抓住每次点击进入列表视图并记住你在那里选择/设置的内容。

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();
}

答案 1 :(得分:1)

将适配器更改为以下内容,只需在适配器上调用getSelected即可。

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;
}
}