【问题标题】:ListView.getCheckedItemPositions not able to return checked Items in SparseBooleanArrayListView.getCheckedItemPositions 无法在 SparseBooleanArray 中返回选中的项目
【发布时间】:2011-09-15 05:30:57
【问题描述】:

我正在尝试读取具有多个checkboxes 的列表中的联系人,但是当我调用sparsebooleanarray..它只会为所有列表条目返回false,..即使是选中的那个...我查看了这个线程Why is ListView.getCheckedItemPositions() not returning correct values? ...但是当我实现 addClickHandlerToCheckBox 时它会强制停止..这已经困扰了我 4 天..请帮助..

 public void populateContactList() {
    // Build adapter with contact entries
    final Cursor cursor = getContacts();
    String[] fields = new String[] {
            ContactsContract.Data.DISPLAY_NAME
    };

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
            fields, new int[] {R.id.contactEntryText});
    mContactList.setAdapter(adapter);
    mContactList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);



    proceedButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {


            SparseBooleanArray checked=mContactList.getCheckedItemPositions();

            int len = mContactList.getCount();


            for(int i=0;i<len;i++)
           {

                if(checked.get(i)==true)
                {

                    String item =  mContactList.getAdapter().getItem(checked.keyAt(i)).toString();
                    edt.append(","+item);

                }


            }
        }



    });                 
}

【问题讨论】:

    标签: android listview checkbox android-layout android-listview


    【解决方案1】:

    我认为你应该使用 List Adapter 视图。

    它会给一个 onCheckedChanged 监听器,所以你也会得到这个位置。

    喜欢...

    holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    int cupos = position+1;
    
                }
            });         
    

    现在看整个例子:

    Listview.setAdapter(new settingsBase(getApplicationContext()));
    
    private class settingsBase extends BaseAdapter{
    
        public settingsBase(Context context) {
           layoutInflater = LayoutInflater.from(context);
        }
    
        @Override
        public int getCount() {
            return listview.length;
        }
    
        @Override
        public Object getItem(int position) {
            return position;
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder ;
         holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    int cupos = position+1;
    
                }
            });         
    
    
            return convertView;
        }
    
        class ViewHolder{
            TextView txtSettingname , txtShow ;
            RadioButton radioButton ;
        }       
    }
    

    试试这个——它将帮助你获得位置以及检查是真是假。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-18
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多