【问题标题】:How to Get the checked item which are cheked from listview?如何获取从列表视图中检查的已检查项目?
【发布时间】:2013-11-27 11:33:49
【问题描述】:

您好,我正在尝试获取选中的项目并将其存储在数据库中,但是当我单击按钮保存以获取每次大小为零时检查的项目的大小时。当单击列表中的项目时,它会给出已检查项目的唯一大小,但如果未选中项目,则仍然如此。 所以它为我创造了问题帮助我谢谢。 这是我的代码是

CustomAdapter.java

public class CustomAdapter extends ArrayAdapter<ContactDetails>{

    List<ContactDetails> listcontacts=null;
    private LayoutInflater mInflater=null;
    private SparseBooleanArray mSelectedItemsIds;

    public CustomAdapter(Activity context, List<ContactDetails> list) {
        super(context, 0);
        mInflater = context.getLayoutInflater();
        mSelectedItemsIds = new SparseBooleanArray(list.size());
        this.listcontacts=list;
    }

    @Override
    public int getCount() {
        return listcontacts.size();
    }

     @Override
     public View getView(int position, View convertView, ViewGroup parent) {

            View view = null;
            if (convertView == null) {

              view = mInflater.inflate(R.layout.contacts_row, null);
              final ViewHolder viewHolder = new ViewHolder();
              viewHolder.txtname = (TextView) view.findViewById(R.id.textView_name);
              viewHolder.txtphonenum = (TextView) view.findViewById(R.id.textView_phonenum);
              viewHolder.checkbox = (CheckBox) view.findViewById(R.id.checkBox_check);
              viewHolder.checkbox.setFocusable(false);
              viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                      ContactDetails element = (ContactDetails) viewHolder.checkbox.getTag();
                      element.setSelected(buttonView.isChecked());


                    }
                  });

              view.setTag(viewHolder);
              viewHolder.checkbox.setTag(listcontacts.get(position));
            } else {
              view = convertView;
              ((ViewHolder) view.getTag()).checkbox.setTag(listcontacts.get(position));
            }
            ViewHolder holder = (ViewHolder) view.getTag();
            holder.txtname.setText(listcontacts.get(position).getName());
            holder.txtphonenum.setText(listcontacts.get(position).getPhonenumber());
            holder.checkbox.setChecked(listcontacts.get(position).isSelected());
            return view;
    }

     private static class ViewHolder {

            TextView txtname;
            TextView txtphonenum;
            CheckBox checkbox;

        }
}

MuListview 类是

    public class AddChoiceContactList extends Activity implements OnClickListener, OnItemClickListener{

        private ListView listview=null;
        private Context context=null;
        private DataBaseHelper db=null;
        CustomAdapter adapter=null;
        private Button btnsave=null;

        List<ContactDetails> list=new ArrayList<ContactDetails>();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.choicelist);
            context=this;

            listview =(ListView)findViewById(R.id.listView_choice);
            btnsave=(Button)findViewById(R.id.button_save);
            btnsave.setOnClickListener(this);
            listview.setOnItemClickListener(this);


        }
        @Override
        protected void onResume() {
            super.onResume();

            list=db.GetDataContacts();
            if(list.size()>0){
                //listview=getListView();
                Toast.makeText(context, "size is: "+list.size(), Toast.LENGTH_LONG).show();
                adapter=new CustomAdapter(AddChoiceContactList.this, list);
                listview.setAdapter(adapter);
                listview.setItemsCanFocus(false);
                listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

            }
            else{
                Toast.makeText(context, "No Contacts Add contats plz!", Toast.LENGTH_LONG).show();
            }
        }


        @Override
        public void onClick(View arg0) {

           SparseBooleanArray checked=listview.getCheckedItemPositions();
 // here m getting size zero when checked then         Toast.makeText(context, "checked size is: "+checked.size(), Toast.LENGTH_LONG).show();
           ArrayList<UpdatedContact> selectedItems = new ArrayList<UpdatedContact>();
           for (int i = 0; i < checked.size(); i++) {
               int position = checked.keyAt(i);
               if (checked.valueAt(i)){
                  ContactDetails detils=adapter.getItem(position);

               } 
           }

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

        }

    }

【问题讨论】:

  • 选中复选框后仍将大小设为零
  • 它在点击项目并检查复选框时给出大小,然后给出大小,但当不点击并选中项目时,它给出大小为零
  • 在数组中添加选中项的代码在哪里?
  • 现在不将项目添加到数组中,因为选中复选框时它的大小为零
  • @sunil 检查我的答案,如果您有任何疑问,请告诉我..

标签: android listview checkboxlist custom-adapter


【解决方案1】:

首先取一个列表ArrayList&lt;ContactDetails&gt; checkedList 为 gloabl 并在构造函数中初始化它..

在自定义适配器中编写如下方法

private ArrayList<ContactDetails> getCheckedItems() {
    return checkedList;
}

并像这样更改您的复选框更改侦听器..

checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            checkedList.add(list.get(position));
        }else {
            checkedList.remove(list.get(position));
        }   
        }
    });

并在您的onItemClickListener 或您的Activity 中调用adapter.getCheckedItems();

然后你会得到所有选中的项目..

【讨论】:

  • 位置已经存在 public View getView(int position, View convertView, ViewGroup parent) 传递该 int 值并将其更改为 final..
【解决方案2】:

请尝试类似的方法。

  1. 在您的主要活动上使用静态 HashMap。
  2. 在您的活动上实现两个方法,AddSelection 和 RemoveSelection
  3. 在您的适配器上,实现 onCheckedStateChanged 侦听器,并根据列表中的位置在地图中执行添加或删除操作。

在您的活动中:

    private static HashMap<Integer,Integer> selectedPositions = new HashMap<Integer,Integer>();
    public static boolean isSelected(int position){
        System.out.println("#### Position: " + position + " Value: " + selectedPositions.containsKey(position));
        return selectedPositions.containsKey(position);
    }

    public static void addSelection(int position){
        System.out.println("#### Puttin Position: " + position);
        selectedPositions.put(position,position);
    }

public static void removeSelection(int position){
    System.out.println("#### Removing Position: " + position);
    selectedPositions.remove(position);
}

在您的适配器中:

checkbox.setChecked(MainActivity.isSelected(position));

checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener(){

    @Override
    public void onCheckedChanged(CompoundButton view, boolean state) {
        // TODO Auto-generated method stub
        if(state == true){
            MainActivity.addSelection(position);
        }else{
            MainActivity.removeSelection(position);
        }
    }

});

现在就是这样,每当您想要遍历 hasmap 以查找所选项目时,请确保在工作完成后将其清除,否则您最终会一次又一次地堆积所选位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 2011-12-10
    相关资源
    最近更新 更多