Reputation: 886
I have customised list view using BaseAdapter, I want to give separate color for the row position%5 ==0. but it also change the color some other rows which not satisfy the condition. Following is my getView().
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view=convertView;
if(convertView==null){
view=inflater.inflate(R.layout.groupl_ist, null);
}
TextView countryName=(TextView) view.findViewById(R.id.tvCountryText);
ImageView countryImage=(ImageView) view.findViewById(R.id.imgCountry);
String label=countryNames.get(position);
countryName.setText(label);
if(position%5==0){
countryImage.setVisibility(LinearLayout.GONE);
view.setBackgroundColor(Color.GREEN);
}else{
countryImage.setImageResource(R.drawable.ic_launcher);
}
return view;
}
Upvotes: 0
Views: 447
Reputation: 1974
I am not pretty sure why is happened your problem but try to set to the original color to the rows that don't satisfy the condition. Also, you should implement the ViewHolder pattern. There are a lots post about this pattern.
if(position%5==0){
view.setBackgroundColor(Color.GREEN);
}else{
view.setBackgroundColor(Color.WHITE); //whatever color
}
Upvotes: 2
Reputation: 2290
It looks like you're having an issue with recycling. You need to implement the ViewHolder pattern. See this link for details on how to implement.
Ultimately what you'll be doing is holding onto the view as you scroll up and down so that it redraws correctly.
Upvotes: 2