Skip to content Skip to sidebar Skip to footer

How To Prevent Modified Listview Item From Being Reused, Or At Least Not Before Reinitialization

Inside MyListViewAdapter I make changes to the convertView visually, but these changes persist on other row items as the list gets reused. In other words now every forth row is red

Solution 1:

Use ViewHolder pattern:

@OverridepublicViewgetView(int position, View convertView, ViewGroup parent){
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_row, null);
            holder = newViewHolder();
            holder.button = (Button)convertView.findViewById(R.id.button);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        holder.button.setOnclickListener(newOnClickListener(){

           publicvoidonClick(View v){
             v.setBackgroundColor(Color.RED);
           }


      });

        return convertView;
    }

privateclassViewHolder{
   publicButton button;
}

Solution 2:

You will have to set it manually, and than save the red position on something like a holder and verify to choose the right color.

Solution 3:

General suggestion, you should read a bit more on the adapters before coding stuff.

Simple:

  1. I told you before to only create one onClickListener. You're wasting A LOT of memory
  2. Only do initialisation work if convertView is null.
  3. You have to keep track of which views were clicked, and on every getView set/reset the color.

       View getView(View convertView, int position etc)
        {
             if(convertView == null{
                 convertView = // inflate it
                 colorStatusMap.put(h.position, defaultColor);
                 convertView.findViewById(R.id.myButton).setOnClickListener(clickListener);
                 convertView.findViewById(R.id.myButton).setTag(convertView);
             }
             convertView.setBackgroundColor(colorStatusMap.get(position));
             convertView.setTag((Integer)position);
    
    
        }
    
        private OnClickListener clickListener = new OnClickListener(){
    
                  publicvoid onClick(View v){
                    View containingView = v.getTag();
                    containingView.setBackgroundColor(Color.RED);
                    int position = (Integer)containingView.getTag();
                    colorStatusMap.put(position, Color.RED);
                  }
        }
        private HashMap<Integer, Integer> colorStatusMap = new HashMap<Integer, Integer>();
    

ps.: I typed all this by heart and without actually compiling and checking it, but you can get an idea of you should do. On the unlikely case of any errors on my code, you can get some fun fixing them.

Solution 4:

try this once

Here is a little code for your help: I will create a boolean arraylist.

private ArrayList<Boolean> item_clicked = new ArrayList<Boolean> ();

Then I will set states of all row elements clicked state as false in my constructor:

for (int i=0; i < no_of_elements.size(); i++) {
    item_clicked.add(i, false);
}

Set the actual clicked state when row clicked: myButton.setOnclickListener( new OnClickListener(){

publicvoidonClick(View v){
         item_clicked.set(position, true);
         View containingView = myButton.getTag();
         containingView.setBackgroundColor(Color.RED);
       }


  });

In your getview method give this condition

if (item_clicked.get(position)) {
//SET BACKGROUD AS RED
} else {
// SET BACKGROUND AS NORMAL COLOR
}

Post a Comment for "How To Prevent Modified Listview Item From Being Reused, Or At Least Not Before Reinitialization"