Android- Save checkbox state in listview

前端 未结 2 1602
独厮守ぢ
独厮守ぢ 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<Boolean> itemChecked1 = null;  
      public CardDetailsAdapter( Context context, ArrayList<CardDetails> results) {
        super( context, R.layout.welcomelistview, R.id.BankNameID, results );
        searchArrayList = results;
        getContext = context;
        mInflater = LayoutInflater.from(context) ;
        itemChecked1 = new ArrayList<Boolean>();
        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);
                }
            }
        });
    
    0 讨论(0)
  • 2021-01-16 16:44
    public View getView(final int position, View convertView,ViewGroup paramViewGroup)
    {
        final ViewHolder holder;
        LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
        if (convertView == null) 
        {
            convertView=inflater.inflate(R.layout.addressbook, null);holder = new ViewHolder();
            holder.affffdress_name=(TextView)convertView.findViewById(R.id.addressbook_name);
            holder.affffdress_number=(TextView)convertView.findViewById(R.id.address_number);
            holder.affffdress_name.setText(affffdressbook_name.get(position).toString().trim())
            holder.affffdress_number.setText(addressbook_number.get(position).toString().trim());
            holder.addressbook_check=(CheckBox)convertView.findViewById(R.id.address_check);convertView.setTag(holder); 
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }
    
        holder.addressbook_check.setId(position);
    
        holder.addressbook_check.setOnClickListener(new OnClickListener()
            {
                public void onClick(View v) 
                {
                    CheckBox cb = (CheckBox) v;
                    int id = cb.getId();
                    if (thumbnailsselection[id])
                    {
                        cb.setChecked(false);
                        thumbnailsselection[id] = false;
                    }
                    else
                    {
                        cb.setChecked(true);
                        thumbnailsselection[id] = true;
                    }
                }
            });
    
        holder.addressbook_check.setChecked(thumbnailsselection[position]);
        return convertView;
    }
    
    class ViewHolder
    {  
        TextView affffdress_name;
        TextView affffdress_number;
        CheckBox addressbook_check;
    }
    
    0 讨论(0)
提交回复
热议问题