我已经通过谷歌和stackoverflow进行了修改..
Android - 具有 2 种不同颜色的 ListView
我发现这篇文章对于更改背景颜色很有用.. 但是如果我想根据逻辑更改背景颜色怎么办?
例如,我的代码已经设置了一个 convertView。像下面
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
convertView = inflater.inflate(R.layout.rest_list, null);
TextView id = (TextView)convertView.findViewById(R.id.id); // title
TextView name = (TextView)convertView.findViewById(R.id.name); // artist name
TextView area = (TextView)convertView.findViewById(R.id.area); // duration
TextView type = (TextView)convertView.findViewById(R.id.type); // duration
ImageView image=(ImageView)convertView.findViewById(R.id.image); // thumb image
HashMap<String, String> restaurant = new HashMap<String, String>();
restaurant = data.get(position);
// Setting all values in listview
if(name != null){
id.setText(restaurant.get(RestaurantList.TAG_ID));
name.setText(restaurant.get(RestaurantList.TAG_NAME));
area.setText(restaurant.get(RestaurantList.TAG_AREA));
type.setText(restaurant.get(RestaurantList.TAG_TYPE));
imageLoader.DisplayImage(restaurant.get(RestaurantList.TAG_IMAGE), image);
}
//this line is example..
convertView.setBackgroundColor(position % 2 == 0 ? Color.WHITE : Color.GREEN);
// i wanna do like this (if there is name MacDonal in NewYork , set the background to green. otherwise leave it white.
// it gives me red underline at the 'logic operation' sentence.
convertView.setBackgroundColor(name == MacDonal && area == newYork ? Color.WHITE : Color.GREEN);
return convertView;
}
有谁知道如何解决?谢谢!