【问题标题】:How to get values from a multichoice listview alertdialog如何从多选列表视图警报对话框中获取值
【发布时间】:2014-01-07 10:38:53
【问题描述】:

我有一个 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;
    }

}

【问题讨论】:

    标签: java android


    【解决方案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;
    }
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 当复选框和列表视图在显示活动的对话框中时,可以应用此解决方案。我的情况是我的列表视图在活动中..而复选框在适配器中。 .但是无论如何通过你和Vipul的解决方案我确实得到了想法..并实现了它..非常感谢..
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-29
      • 1970-01-01
      • 2016-06-16
      • 2022-08-10
      • 2013-03-23
      • 2012-09-10
      相关资源
      最近更新 更多