ListView Checkbox State Item

I am using a checkbox with every list item and when the user clicks on any checkbox, I save that list item in the SQLite database, but whenever I restart my application without getting the check for the list items I checked earlier.

So how do you store the state in a checkbox?

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // convert view = design
    View v = convertView;

    if (v == null) {

        holder = new ViewHolder();
        v = vi.inflate(Resource, null);

        holder.tvName = (TextView) v.findViewById(R.id.textView1);          
        holder.checkBox = (CheckBox) v.findViewById(R.id.cbBox);

        boolean strDataExist = activity.myDb.Exists(actorList.get(position).getName());
        if(strDataExist)
        {
            actorList.get(position).setChecked(true);
        }
        else
        {
            actorList.get(position).setChecked(false);
        }

        v.setTag(holder);

    } 
    else 
    {
        holder = (ViewHolder) v.getTag();           
    }           

      

+3


source to share


4 answers


In the getView method, the code should be



    if(strDataExist)
    {
        holder.checkBox.setChecked(true);
    }
    else
    {
        holder.checkBox.setChecked(false);
    }

      

+3


source


You have to check all data before inflating whether this data is in the DB or not, based on the result you have to check the value of the checkbox, you do not check.

You need to check



holder.checkBox.setChecked(true/false);

      

+4


source


I'm not sure about your question, but I will try to help you.

I think you want to put yours into action checkbox

with checked

or not depend if you previously saved it to database

.

In the method, getView

you have to check if the service exists in the database using the method public boolean Exists(String strServiceName)

(you use lower case for the method name).

...

holder.checkBox = (CheckBox) v.findViewById(R.id.cbBox);
holder.checkBox.setChecked(exists(actorList.get(position).getName().toString()));

      

Sincerely.

UPDATE If you want to keep the status of the service if the service name exists in the database. This solution should be a solution:

getView(...){
 //...
 if (databaseHandler.exists(serviceName)){
  Service service = new Service(serviceName, holder.checkBox.isChecked());
  databaseHandler.save(service);
 }
}

      

0


source


try this way, it worked for me

public class CustomAdapter extends BaseAdapter {
    private final LayoutInflater inflater;
    private final Context context;
    private List<ModelPooja> listData;

    public CustomAdapter(Context mainActivity, List<ModelPooja> listData) {
        context = mainActivity;
        this.listData = listData;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_item_poojaselection, null);
            holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname);
            holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.checks.setOnCheckedChangeListener(null);
        holder.checks.setFocusable(false);

        if (listData.get(position).isselected) {
            holder.checks.setChecked(true);
        } else {
            holder.checks.setChecked(false);
        }

        holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton cb, boolean b) {

                if (checkMaxLimit()) {

                    if (listData.get(position).isselected && b) {
                        holder.checks.setChecked(false);
                        listData.get(position).isselected = false;

                    } else {
                        holder.checks.setChecked(false);
                        listData.get(position).isselected = false;
                        Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    if (b) {
                        listData.get(position).isselected = true;
                    } else {
                        listData.get(position).isselected = false;
                    }
                }
            }
        });

        holder.tv.setText(listData.get(position).getPOOJA_LISTING_NAME());
        return convertView;
    }

    public boolean checkMaxLimit() {
        int countermax = 0;
        for(ModelPooja item : listData){
            if(item.isselected){
                countermax++;
            }
        }
        return countermax >= 5;
    }

    public class ViewHolder {
        TextView tv;
        public CheckBox checks;
    }
}

      

0


source







All Articles