简体   繁体   English

ListView复选框保存状态

[英]ListView Checkbox Save State

I am using Checkbox with each and every list item, and when user do tap on any of the checkbox i am storing that list item into SQLite database, but whenever i do restart my app not getting check for list items those i checked earlier. 我正在使用Checkbox与每个列表项,当用户点击任何复选框我将该列表项存储到SQLite数据库,但每当我重新启动我的应用程序时,不检查我之前检查过的列表项。

So How do I save state to Checkbox ? 那么如何将状态保存到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();           
    }           

You should check every data before inflating whether that data is in DB or not, on the base of result you should check the value of checkbox, you are not checking. 你应该在膨胀之前检查每个数据是否在数据库中,在结果的基础上你应该检查复选框的值,你没有检查。

You need to check 你需要检查一下

holder.checkBox.setChecked(true/false);

In getView method, the code should be 在getView方法中,代码应该是

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

I am not sure about your question, but I'm going to try help you. 我不确定你的问题,但我会尽力帮助你。

I think that you want to inicializate your checkbox with checked or not depending if you have saved into database previously. 我认为您希望使用checked或不依赖于您的checkbox ,具体取决于您之前是否已保存到database

In the method getView you should check if the service exists in the database with method public boolean Exists(String strServiceName) (you shoud use lowercase for method's name). 在方法getView您应该使用方法public boolean Exists(String strServiceName)检查数据库中是否存在服务(您应该使用小写作为方法名称)。

... ...

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

Regards. 问候。

UPDATE If you want to save the service status if the the service name exists in the database. 更新如果要在数据库中存在服务名称时保存服务状态。 That coul be a solution : 这可以解决问题:

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

try this way,this 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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM