【问题标题】:Know the clicked checkBox Items in ListView?知道 ListView 中被点击的复选框项目吗?
【发布时间】:2013-12-20 06:38:03
【问题描述】:

我有一个包含一些元素和一个复选框的自定义列表视图。当我点击一个按钮时。我想知道已检查的元素的位置。 以下是我的代码

public class Results extends ListActivity implements OnClickListener{
    String[] donorName,donorPhone;
    int totNumber;
    Button callBut;
    ListView listView;
    List<RowItem> rowItems;
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.results);
        Intent intent = getIntent();
          donorName = intent.getStringArrayExtra("name");
          donorPhone = intent.getStringArrayExtra("phone");
          totNumber = intent.getExtras().getInt("totDonors");
          callBut = (Button)findViewById(R.id.callBut);
          callBut.setOnClickListener(this);
          rowItems = new ArrayList<RowItem>();
            for (int i = 0; i < totNumber; i++) {
                RowItem item = new RowItem(donorName[i], donorPhone[i]);
                rowItems.add(item);
            }

            ListAdapter adapter = new MySimpleArrayAdapter(this,
                    R.layout.list_item, rowItems);
            setListAdapter(adapter);


    };
    ///////////////////////////////////////////////////////////////////////////////////////////
    public static class MySimpleArrayAdapter extends ArrayAdapter<RowItem> implements OnCheckedChangeListener {


          Context context;
          static List<RowItem> donorList = new ArrayList<RowItem>();

            public MySimpleArrayAdapter(Context context, int resourceId,
                    List<RowItem> donorList) {
                super(context, resourceId, donorList);
                this.context = context;
                this.donorList = donorList;
            }
            private class ViewHolder {
                Button donorCall,exp;
                TextView donorName;
                TextView donorPhone;
                CheckBox chkBox;

            }
          @Override
          public View getView(final int position, View convertView, ViewGroup parent) {
              ViewHolder holder = null;
                final RowItem rowItem = getItem(position);
            LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list_item, null);
                holder = new ViewHolder();
                holder.donorName = (TextView) convertView.findViewById(R.id.donorName);
                holder.donorPhone = (TextView) convertView.findViewById(R.id.donorPhone);
                holder.donorCall = (Button) convertView.findViewById(R.id.donorCall);
                holder.chkBox = (CheckBox) convertView.findViewById(R.id.chkBox);
                convertView.setTag(holder);

            }

            else
                holder = (ViewHolder) convertView.getTag();

            holder.donorPhone.setText(rowItem.getdonorPhoneS());
            holder.donorName.setText(rowItem.getdonorNameS());
           holder.chkBox.setTag(position);
           holder.chkBox.setOnCheckedChangeListener(this);
            holder.donorCall.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Log.d("Button Clicked",position+"");
                    Intent startCall = new Intent(Intent.ACTION_CALL);

                    startCall.setData(Uri.parse("tel:" + rowItem.getdonorPhoneS()));
                    context.startActivity(startCall);
                }

            });
            return convertView;
          }
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            int position = (Integer) buttonView.getTag();
               if (isChecked) {
                   donorList.get(position).setSelected(true);
                   Log.d("Tag",donorList.get(position).isSelected()+"");
               } else {
                   buttonView.setSelected(false);
                   Log.d("Unchecked",isChecked+"");
               }
               notifyDataSetChanged();
        }

    }
////////////////////////////////////////////////////////////////////////////////////////////////    
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        String msgRecipient;
        Log.d("MSg","Button Clicked");
        for (int x = 0; x<totNumber;x++){
            if(MySimpleArrayAdapter.donorList.get(x).isSelected()){
                Log.d("position Checked",x+"");
            }
            else
                 Log.d("position UnChecked",x+"");  
        }
    }
}

当我点击一个项目上的复选框时,我在 Log 中得到 true。但是当我点击 Button 时,所有元素都显示在未选中状态下。

【问题讨论】:

    标签: java android listview checkbox


    【解决方案1】:

    您忘记在 getView 中设置复选框的选中状态,因此如果您向下/向上滚动,您会看到旧的复选框不会被更新。

    您需要做的是拥有一组整数(或 sparseIntArray,这更好),选中复选框时将项目位置添加到其中,并在未选中时删除。

    为了获得所有选中的复选框,只需使用这组整数...

    【讨论】:

      【解决方案2】:

      您可以尝试这个来了解适配器中选定项目的位置。

                      for(int i=0;i<MySimpleArrayAdapter.mCheckStates.size();i++)
                      {
                          if(MySimpleArrayAdapter.mCheckStates.get(i)==true)
                          {
                                // i is the position of a checked items
      
                          }
      
                      }
      

      【讨论】:

        【解决方案3】:

        这里可能会出现一些问题。

        首先,您需要确保您的 ListView(ListActivity 内部)的 setChoiceMode 不是默认值。即:

        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        

        查看详情:Losing current selection when calling adapter.notifyDataSetChanged()

        此外,您对 notifyDataSetChanged() 的调用可能会导致您的选择被重置,如下所述:Losing current selection of list item after updating the listview items using notifyDataSetChanged

        【讨论】:

          猜你喜欢
          • 2012-04-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-14
          相关资源
          最近更新 更多