Listview's checked items not there after scrolling

前端 未结 3 1329
挽巷
挽巷 2021-01-07 06:48

I have created a listview with textview and checkbox. However after scrolling the list down(or up) all the checked items in the list get unselected. How to make the checked

3条回答
  •  长发绾君心
    2021-01-07 07:09

    You can use this adapter class and get selected checkbox id by using listview.getPositionForView(cBox)

       public class aAdapter extends SimpleAdapter {
        //private List tables;
        SharedPreferences prefs;
        private Activity activity;
        String val = "";
    
        //@SuppressWarnings("unchecked")
        public aAdapter(Activity context, List> tables, int resource, String[] from,
                int[] to) {
            super(context, tables, resource, from, to);
            //this.tables = (List
    ) tables; activity = context; } public View getView(final int position, final View convertView, ViewGroup parent) { View row = super.getView(position, convertView, parent); if (row == null) { LayoutInflater inflater = (LayoutInflater) activity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); row = inflater.inflate(R.layout.reports_list, null); } final CheckBox cBox=(CheckBox)row.findViewById(R.id.cb1); if(bulkflag) { cBox.setVisibility(View.VISIBLE); } else { cBox.setVisibility(View.GONE); } cBox.setOnClickListener(new OnClickListener() { public void onClick(View v) { if(cBox.isChecked()) { //selectedIds.add(recIdArr.get(reportslistview.getPositionForView(cBox))); System.out.println("position "+reportslistview.getPositionForView(cBox)+" Checked"); } else { System.out.println("position "+reportslistview.getPositionForView(cBox)+" UnChecked"); } } }); return row; } }

    提交回复
    热议问题