0

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. Now what? How do I prevent this?

 View getView(View convertView, int position etc)
 {
      Button myButton = (Button)convertView.findViewById(R.id.myButton);
      myButton.setTag(convertView);
      myButton.setOnclickListener( new OnClickListener(){

           public void onClick(View v){
             View containingView = myButton.getTag();
             containingView.setBackgroundColor(Color.RED);
           }


      });

 }

The problem is the view gets reused so now I have a red list item elsewhere and not just where for the cell representing the view I intended. Whats the solutions for this? How to keep view from getting reused in this manner?

Nermeen
  • 15,883
  • 5
  • 59
  • 72
user1847544
  • 957
  • 2
  • 9
  • 17
  • Means you are facing problem when you are clicking on any row and moving your listview items. Is it randomly making any row with red color? – Bharat Sharma Dec 19 '12 at 11:19
  • I think the views get reused for list view. Thats whats happening so pattern of every fouth row reuses the view. – user1847544 Dec 19 '12 at 11:59
  • Actually similar problem i was facing with check boxes in which checkboxes got checked automatically so here is that link you can check this i will also try to post some code..http://stackoverflow.com/questions/10190083/how-to-implement-a-button-that-gets-all-checkboxs-state-and-adds-the-value-of-c/10191369#10191369 – Bharat Sharma Dec 19 '12 at 12:03

4 Answers4

0

Use ViewHolder pattern:

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

        holder.button.setOnclickListener(new OnClickListener(){

           public void onClick(View v){
             v.setBackgroundColor(Color.RED);
           }


      });

        return convertView;
    }

private class ViewHolder{
   public Button button;
}
Veaceslav Gaidarji
  • 4,261
  • 3
  • 38
  • 61
0

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.

Pozzo Apps
  • 1,859
  • 2
  • 22
  • 32
0

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(){
    
                  public void 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.

Budius
  • 39,391
  • 16
  • 102
  • 144
0

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(){

       public void onClick(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
}
Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
Bharat Sharma
  • 3,926
  • 2
  • 17
  • 29