我已經通過谷歌修改和計算器..ListView的背景顏色邏輯
Android - ListView with 2 different Colors
我發現這個職位改變背景顏色有用的..但如何,如果我想改變根據邏輯背景顏色?
例如我的代碼已經設置了一個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;
}
任何人都知道如何解決?謝謝!
完美。此代碼有效!非常感謝Naveen!我會消化它。 = d – 2013-03-22 06:39:30