簡體   English   中英

如何從multichoice listview alertdialog獲取值

[英]How to get values from a multichoice listview alertdialog

我有一個PersonList的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;
    }

}

我自己創建了一個多重對話框,這就是我正在做的事情,基本上你需要抓住每次點擊進入列表視圖並記住你在那里選擇/設置的內容。

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

將適配器更改為以下,並在適配器上調用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;
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM