问题
final SimpleAdapter adapter = new SimpleAdapter(getActivity(),
itemlist, R.layout.listview_custom_view_sales_products,
new String[] { "Number", "item_name", "qty" }, new int[] {
R.id.number, R.id.Name, R.id.value }) {
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
HashMap<String, String> obj = (HashMap<String, String>) getListView().getAdapter().getItem(position);
for (String x : added_items) {
if (String.valueOf(x).equals(obj.get("item_name").toString())) {
Log.i("sdsds", "working");
view.setBackgroundColor(Color.BLUE);
}
}
return view;
};
};
Here I used the above code to color items according to the items included in addeditem(it is array).But my problem is that it changes the color of the other row views which were not newly added in my wArrayList.
回答1:
You have to do:
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
String word=yourArrayOfStrings[position];
if(word.equals("item_...")){
view.setBackgroundColor(Color.BLUE);
}
return view;
}
来源:https://stackoverflow.com/questions/21372920/android-change-the-color-of-list-view-item