Reputation: 54969
I am trying to Set alternative Background Color for the List View and Its Randomly setting the Background Colors every time i start that activity.
Array Adapter
public class PlacesListAdapter extends ArrayAdapter<Place> implements
Filterable {
public Context context;
private List<Place> orig, itemDetailsrrayList;
private PlaceFilter filter;
public PlacesListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public PlacesListAdapter(Context context, int resource, List<Place> places) {
super(context, resource, places);
this.context = context;
// this.places = places;
itemDetailsrrayList = places;
orig = new ArrayList<Place>(itemDetailsrrayList);
filter = new PlaceFilter();
// imageLoader = new ImageLoader(context.getApplicationContext());
}
public int getCount() {
return itemDetailsrrayList.size();
}
public Place getItem(int position) {
return itemDetailsrrayList.get(position);
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
// View view = convertView;
// Place p = places.get(position);
if (convertView == null) {
LayoutInflater viewInflater;
viewInflater = LayoutInflater.from(getContext());
convertView = viewInflater.inflate(R.layout.list_item_place, null);
holder = new ViewHolder();
holder.placeTitle = (TextView) convertView
.findViewById(R.id.place_title);
holder.placeDistance = (TextView) convertView
.findViewById(R.id.place_distance);
holder.placeCategoryIcon = (ImageView) convertView
.findViewById(R.id.place_category_icon);
// Setting Alternative Row Colors
if (position % 2 == 0) {
convertView
.setBackgroundResource(R.drawable.list_view_place_row_1);
} else {
convertView
.setBackgroundResource(R.drawable.list_view_place_row_2);
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.placeTitle.setText(itemDetailsrrayList.get(position)
.getPlaceTitle());
holder.placeDistance.setText("200");
holder.placeCategoryIcon.setImageResource(R.drawable.icon_category);
return convertView;
}
static class ViewHolder {
TextView placeId;
TextView placeTitle;
TextView placeDistance;
ImageView placeCategoryIcon;
}
@Override
public Filter getFilter() {
// TODO Auto-generated method stub
return filter;
}
private class PlaceFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults oReturn = new FilterResults();
ArrayList<Place> results = new ArrayList<Place>();
if (orig == null)
orig = itemDetailsrrayList;
if (constraint != null) {
if (orig != null && orig.size() > 0) {
for (Place g : orig) {
if (g.getPlaceTitle()
.toLowerCase()
.startsWith(constraint.toString().toLowerCase()))
results.add(g);
}
}
oReturn.values = results;
}
return oReturn;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
itemDetailsrrayList = (ArrayList<Place>) results.values;
notifyDataSetChanged();
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/list_view_place_row_1" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@color/list_view_place_selected" android:state_pressed="true" android:state_selected="false"/>
<item android:drawable="@color/list_view_place_selected" android:state_pressed="false" android:state_selected="true"/>
<item android:drawable="@android:color/transparent"/>
</selector>
Upvotes: 3
Views: 1568
Reputation: 6604
Before returning the convertView,set the background as you want. Use this line just before returning the convertView
if (position % 2 == 0)
{
convertView .setBackgroundResource(R.drawable.list_view_place_row_1);
}
else
{
convertView .setBackgroundResource(R.drawable.list_view_place_row_2);
}
return convertView;
I am not sure mate,, but I think that it will go inside the (convertView == null)
for the first time. After that when you call holder = (ViewHolder) convertView.getTag();
then it request for the view, which have the previous position background color. Thats why you were getting that output
Upvotes: 1
Reputation: 661
in the getview method, you can implement one logic, i.e if the position is even no then set background color1 and if the position is odd then set background color2.
Upvotes: 2
Reputation: 3261
Code seems correct. May be try following :
Declare integer array of colors:
private int[] colors = new int[] { R.drawable.list_view_place_row_1, R.drawable.drawable.list_view_place_row_2};
And in getView()....
int colorPos = position % colors.length;
convertView.setBackgroundResource(colors[colorPos]);
Upvotes: 3