1

I am trying to display detailed Product information in an custom Listview with two TextViews per row for the key/value pair. The data is displayed correct. I also colored every second line different.

And there is my Problem. If I scroll up and down the different colored rows change their color and remain in this state. The data is not affected from this problem. Just the backroundcolor of the TextViews. I use the ViewHolder Pattern but this did not change anything. I added the code of the adapter. I think thats enough. Have you any idea?

Screenshot of the problem:

enter image description here

Code:

public class ProductDetailAdapter extends BaseAdapter {

private LinkedHashMap<String,String> list;
private Context context;

public ProductDetailAdapter(Context c, LinkedHashMap<String,String> list){
    super();
    this.context = c;
    this.list=list;

}
@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

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

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    ProductDetailAdapter.ViewHolder viewHolder;

    if(convertView == null){
        convertView=inflater.inflate(R.layout.product_detail_data_row,null);
        viewHolder = new ViewHolder();
        viewHolder.textViewKey = (TextView) convertView.findViewById(R.id.productDataKey);
        viewHolder.textViewValue = (TextView) convertView.findViewById(R.id.productDataValue);
        convertView.setTag(viewHolder);

    }else {
        viewHolder = (ProductDetailAdapter.ViewHolder) convertView.getTag();
    }

    viewHolder.textViewKey.setText((String)list.keySet().toArray()[position]);
    viewHolder.textViewValue.setText(list.get(list.keySet().toArray()[position]));

    if(position % 2 == 0){
        viewHolder.textViewKey.setBackgroundColor(context.getResources().getColor(R.color.colorParkerWhite2));
        viewHolder.textViewValue.setBackgroundColor(context.getResources().getColor(R.color.colorParkerWhite2));
    }

    return convertView;
}

private static class ViewHolder {

    public TextView textViewKey;
    public TextView textViewValue;

    public ViewHolder(){};

}
}
Mike
  • 67
  • 1
  • 11

3 Answers3

4

That happens because the rows are being recycled. It's a common problem.

You can solve it by doing:

if(position % 2 == 0){
    viewHolder.textViewKey.setBackgroundColor(context.getResources().getColor(R.color.colorParkerWhite2));
    viewHolder.textViewValue.setBackgroundColor(context.getResources().getColor(R.color.colorParkerWhite2));
} else {
    viewHolder.textViewKey.setBackgroundColor(context.getResources().getColor(R.color.colorParkerWhite1)); //Or the color that you want for odd rows
    viewHolder.textViewValue.setBackgroundColor(context.getResources().getColor(R.color.colorParkerWhite1)); //Or the color that you want for odd rows
}
Gustavo Conde
  • 927
  • 12
  • 20
0

Try this:

super( c, 0, list );

instead of this:

super();

Once you pass the data source to the adapter you no longer need :

getCount

getItem

getItemId

Community
  • 1
  • 1
RonTLV
  • 2,376
  • 2
  • 24
  • 38
0

please refer this link and link2 and link3 these will work for you

Community
  • 1
  • 1
vijay chhalotre
  • 396
  • 2
  • 11