There is a problem reloading the saved Checkbox value.

Asked 2 years ago, Updated 2 years ago, 154 views

Hello. I'm developing an Android app, and after selecting a check box in the Listview in my app, I need to put a specific text in the ArrayList and save that part, and then turn it back on using the saved part, the same check box should be checked.

I checked with the following code using the for statement.

for(int i=0; i<TargetList.size(); i++) {
                if(TargetList.get(i).equals(appListItem.PackageName)) {
                    if(holder.checkBox.isChecked()) {
                        holder.checkBox.setChecked(false);
                    }
                    else {
                        holder.checkBox.setChecked(true);
                    }
                }
            }

However, when I checked it like this, it seemed to work well, but sometimes there was a problem that the wrong check box was released, not the check box I pressed. I've been thinking about it for hours, but I don't know how to fix it ㅠ<

The entire source code of getView for the adapter in question.

    @Override
    public View getView(final int pos, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        final appListItem appListItem = mListData.get(pos);
        ArrayList<String> TargetList = Util.getStringArrayPref(context, "TargetList");
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        try {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.select_listview, parent, false);
                holder = new ViewHolder();
                holder.PackageName = (TextView) convertView.findViewById(R.id.packagenames);
                holder.Label = (TextView) convertView.findViewById(R.id.applabel);
                holder.Icon = (ImageView) convertView.findViewById(R.id.appicon);
                holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
                convertView.setTag(holder);
            } } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.PackageName.setText(appListItem.PackageName);
            holder.PackageName.setTag(pos);
            holder.Label.setText(appListItem.Label);
            holder.Label.setTag(pos);

            if (appListItem.Icon != null) {
                holder.Icon.setVisibility(View.VISIBLE);
                holder.Icon.setImageDrawable(appListItem.Icon);
            } } else {
                holder.Icon.setVisibility(View.GONE);
            }

            holder.checkBox.setChecked(((ListView)parent).isItemChecked(pos));
            holder.checkBox.setTag(pos);

            for(int i=0; i<TargetList.size(); i++) {
                if(TargetList.get(i).equals(appListItem.PackageName)) {
                    if(holder.checkBox.isChecked()) {
                        holder.checkBox.setChecked(false);
                    }
                    else {
                        holder.checkBox.setChecked(true);
                    }
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

android listview arraylist java

2022-09-22 20:32

1 Answers

http://stackoverflow.com/questions/37224342/how-save-checkbox-checked-into-database

I've seen the second part of the Stackoverflow article above and solved it! Thank you!


2022-09-22 20:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.