0

While working on list view and finding a problem after inflating list item to view using custom adapter. My problem is a random list item view button text in changed to "Added" instead of displaying text "Add",i have added my adapter code -

view = convertView;
if(view==null){
   view = mLayoutInflater.inflate(R.layout.list_item, null);
   mHolder = new ListHolder();
   mHolder.mNameTV = (TextView) view.findViewById(R.id.user_full_name);
   mHolder.mButton = (Button) view.findViewById(R.id.add);
   view.setTag(mHolder);
}
else{
     mHolder = (ListHolder) view.getTag();
}
mHolder.mNameTV.setText(myList.get(position).getName());
if(myList.get(position).isAdded()){
   mHolder.mButton.setText("Added");
}
 else{
      mHolder.mButton.setText("Add");
 }
return view;

It display correct name in text view but value of button in list item is not correct.

Thanks in Advance.

Ravi Bhandari
  • 4,682
  • 8
  • 40
  • 68
  • http://stackoverflow.com/questions/20611123/listview-subobject-clickable-confilct. your case is similar to this – Raghunandan Jan 07 '14 at 07:46
  • code seems correct.. maybe the problem is in the class that handles the data? – thedjaney Jan 07 '14 at 07:53
  • thedjaney Sir ,i have debug code multiple times and check data flag value and found correct but at run times it dont show correct view. – Ravi Bhandari Jan 07 '14 at 07:58
  • any important reason to use BaseAdapter? – pskink Jan 07 '14 at 08:03
  • try creating one holder per position... – thedjaney Jan 07 '14 at 08:08
  • if i'm not mistaken the whole list uses one instance of the convertView.. if you create depending on view==null, they might get mixed up. – thedjaney Jan 07 '14 at 08:09
  • 1
    set tag to button `mHolder.mButton.setTag(position)` then replace this `if(myList.get(position).isAdded())` with if(myList.get(mHolder.mButton.getTag()).isAdded()) – Ketan Ahir Jan 07 '14 at 08:10
  • thedjaney Sir, i have tried creating new holder per position but still finding same problem.When i click on a single button after that 4-5 button text changed. – Ravi Bhandari Jan 07 '14 at 08:18
  • @ Ketan Sir, i have try as you suggested but after click ing 5th position button,multiple button text is changed in this position sequence- 5,13,21,29,37 and i have total 45 item in list. – Ravi Bhandari Jan 07 '14 at 08:28

3 Answers3

1

After trying different example i have found solution to my problem. I create two different view for it say 1.user_added.xml and 2. user_add.xml. and inside my adapter checking condition and then inflate view like this-

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
    if(myList.get(position).isAdded())
        return ADDED;
    else
        return NOTADDED;
}

and inside getView method -

    int viewType = getItemViewType(position); 
    if(view==null){
        mHolder = new ListHolder();
        if(viewType==NOTADDED){
           view = mLayoutInflater.inflate(R.layout.list_item_not_added, parent,false);
        }
        else{
             view = mLayoutInflater.inflate(R.layout.list_item_added, parent,false);
        }
        mHolder.mNameTV = (TextView) view.findViewById(R.id.user_full_name);
        mHolder.mButton = (Button) view.findViewById(R.id.add);
        view.setTag(mHolder);
     }
     else{
        mHolder = (ListHolder) view.getTag();
     }
      mHolder.mNameTV.setText(myList.get(position).getName());
      mHolder.mButton.setTag(position);
      mHolder.mButton.setOnClickListener(MyListener);
Ravi Bhandari
  • 4,682
  • 8
  • 40
  • 68
0
if(myList.get(position).isAdded()){

Here make sure that, you have not already set true while parsing the data. As you have used the isAdded() method above. It seems that, by default the value of this method is returning true and as a result your button text always comes as "Added".

YuDroid
  • 1,599
  • 5
  • 22
  • 44
  • Sir i have set default value of flag isAdded = false and after button click value of flag is updated to true.When app first run all button show value "Add" and then i click on one button then random number of button text is change to "Added".that is the problem – Ravi Bhandari Jan 07 '14 at 08:12
0

This is happening because of recycling of ListView, so you should store the view and change the text according to the View.

Modify your code something like this,

if(view==null){
   view = mLayoutInflater.inflate(R.layout.list_item, null);
   mHolder = new ListHolder();
   mHolder.mNameTV = (TextView) view.findViewById(R.id.user_full_name);
   mHolder.mButton = (Button) view.findViewById(R.id.add);
   view.setTag(mHolder);
   mHolder.mButton.setTag(position);
}
else{
     mHolder = (ListHolder) view.getTag();
}
mHolder.mNameTV.setText(myList.get(position).getName());
int getPosition = (Integer)mHolder.mButton.getTag();
if(myList.get(getPosition).isAdded()){
   mHolder.mButton.setText("Added");
}
 else{
      mHolder.mButton.setText("Add");
 }
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • Your code does not make any sense. Why adding the same button reference to the tag when you have it in the ViewHolder anyway? – WarrenFaith Jan 07 '14 at 10:49