我试图为列表视图设置替代背景颜色,并在每次开始该活动时随机设置背景颜色.
阵列适配器
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>
解决方法:
在返回convertVIEw之前,请根据需要设置背景.
在返回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;
我不确定伙伴,但我认为它将首次进入(convertVIEw == null).之后你调用holder =(VIEwHolder)convertVIEw.getTag();然后它请求视图,它具有前一个位置背景颜色.这就是为什么你得到那个输出
总结以上是内存溢出为你收集整理的适用于ListView的Android替代颜色是随机的全部内容,希望文章能够帮你解决适用于ListView的Android替代颜色是随机的所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)