ListView Adapter does not recall the status of Checkbox-ov



  • I have a chequebox list, defolt all the elements. false♪ In the chequebox click, the element has to become true and change that parameter in the object, on the list. I suspect the problem with the methodi. setTag() and getTag()
    The evidence is altering, even recording, but when I get to this category, the first data are still on the line.
    The question is, why are old data up and not modified?

    Here's the whole ViewAdapter code:

    public class NewItemListViewAdapter extends ArrayAdapter<NewShop> {
    private OnPlusButtonClickListenner listenner;
    private ArrayList<NewShop> listItems;
    private DataFactory dataFactory = new DataFactory();
    private NewFavoriteFactory favoriteFactory;
    

    public NewItemListViewAdapter(Context context, int resource, List<NewShop> objects) {
    super(context, resource, objects);
    favoriteFactory = new NewFavoriteFactory(context);
    listItems = dataFactory.getListShop(Type.Mountain);
    }

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

    ViewHolder holder = null;
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.new_item_list, parent, false);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    
    Bitmap bitmapFon = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.white_fon);
    Bitmap bitmapIconShop = BitmapFactory.decodeResource(getContext().getResources(), getItem(position).getIconShop());
    holder.icon.setImageBitmap(MyBitMap.getBitmapForCategory(bitmapFon, bitmapIconShop));
    holder.icon.setTag(position);
    holder.icon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int pos = (Integer) v.getTag();
            getItem(pos);
    
            if (listenner != null) {
                listenner.onPlusClick(listItems.get(position).getUrlShop());
            }
        }
    });
    
    holder.favoriteShop.setChecked(getItem(position).getFavoriteShop());
    holder.favoriteShop.setTag(position);
    holder.favoriteShop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int pos = (Integer) v.getTag();
            boolean checked = ((CompoundButton) v).isChecked();
            getItem(pos).setFavoriteShop(checked);
            // real save
            String item = new Gson().toJson(getItem(pos).getId());
            if (checked) {
                Log.d(StringVariables.TEST, "save");
                PrefUtil.save(getContext(), item);
            } else {
                Log.d(StringVariables.TEST, "remove");
                PrefUtil.remove(getContext(), item);
            }
        }
    });
    
    
    holder.name.setText(getItem(position).getNameShop());
    holder.name.setTag(position);
    holder.name.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int pos = (Integer) v.getTag();
            getItem(pos);
    
            if (listenner != null) {
                listenner.onPlusClick(listItems.get(position).getUrlShop());
            }
        }
    });
    
    convertView.setOnClickListener(onClickListener(position));
    
    return convertView;
    

    }

    public void setOnPlusClickListenner(OnPlusButtonClickListenner listenner) {
    this.listenner = listenner;
    }

    private class ViewHolder {
    private ImageView icon;
    private TextView name;
    private CheckBox favoriteShop;

    public ViewHolder(View v) {
        icon = (ImageView) v.findViewById(R.id.iconShop);
        name = (TextView) v.findViewById(R.id.nameShop);
        favoriteShop = (CheckBox) v.findViewById(R.id.checkBoxFavorite);
    }
    

    }
    }

    The data shall be stored as follows:

    ArrayList<NewShop> listBike = new ArrayList<>();

    listBike.add(new NewShop(1, R.drawable.icon_universal, "Velomarket", "http://velomarket.org.ua/", false));
    listBike.add(new NewShop(2, R.drawable.icon_extrim_style, "Екстрем Стайл", "http://extremstyle.ua/", false));
    



  • Read my answer. https://ru.stackoverflow.com/q/477016/17609 ♪ Shortly, you don't need a set/getTag. You need to keep the checkpoints. In getView, check the cheque and check his/her cleaning. When compressed, the method of rewriting the components of the adapter shall be triggered.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2