Android- Save checkbox state in listview

前端 未结 2 1605
独厮守ぢ
独厮守ぢ 2021-01-16 16:23

i have listview with checkboxes and \"save\" button.
Whichever checkboxes i check and click save button, i want to save the data related to those checked rows into datab

2条回答
  •  滥情空心
    2021-01-16 16:42

    Hi in the GetCardDetails() method add sr1.setSelected(true) or sr1.setSelected(false)

    in adapter add the following

      public static ArrayList itemChecked1 = null;  
      public CardDetailsAdapter( Context context, ArrayList results) {
        super( context, R.layout.welcomelistview, R.id.BankNameID, results );
        searchArrayList = results;
        getContext = context;
        mInflater = LayoutInflater.from(context) ;
        itemChecked1 = new ArrayList();
        for (int i = 0; i < results.size(); i++) {
            itemChecked1.add(i, results.get(i).isSelected()); // initializes all items value with false         
        }
    }
    

    in getview

    holder.chkbox.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                CheckBox cb = (CheckBox) v;
                CardDetails planet = (CardDetails) cb.getTag();
                //planet.setSelected(cb.isChecked());
                if (holder.chkbox.isChecked()) {
                    itemChecked1.set(position, true);
                    holder.chkbox.setChecked(true);
                    planet.setSelected(true);
                } else if (!holder.chkbox.isChecked()) {
                    itemChecked1.set(position, false);
                    holder.chkbox.setChecked(false);
                    planet.setSelected(false);
                }
            }
        });
    

提交回复
热议问题