0
我在更改列表視圖中特定行的背景顏色時感到困惑,下面是我嘗試的代碼。當我滾動列表時,不同的行會突出顯示,我想了解背後的原因。邏輯似乎很簡單,但結果是不可預測的。我該怎麼做到這一點。更改Android自定義ListView中特定行的背景色難度
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.rows_for_layout, null);
holder = new ViewHolder();
holder.name = (TextView)convertView.findViewById(R.id.name);
holder.rated=(ImageView)convertView.findViewById(R.id.rated);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
//selected_position is the position where the list has to be highlighted
if(position==selected_position){
holder.name.setText(elements.get(position).get("name"));
convertView.setBackgroundResource(R.drawable.highlight_this);
holder.rated.setBackgroundResource(R.drawable.star_image);
}else{
holder.name.setText(elements.get(position).get("name"));
}
return convertView;
}//getView ![alt text][1]
看不出任何代碼的問題,當你改變/更新「selected_position」變量? – 2010-11-29 10:38:00
我在onCreate()方法中設置變量'selected_position'的值,以供參考,請查看此網址的完整代碼http://pastebin.com/ki7q6Wy0 – ganesh 2010-11-29 11:19:20