我有列表视图。通过适配器设置列表项时,我在 getView() 中为特定位置的项目设置了背景。当我单击列表项时,我需要为该项目设置背景,并删除我在适配器类的 getView() 中设置的项目的背景。我已经实现了在单击列表项时设置背景。但我无法删除我在 getView() 中设置的背景。我没时间了。需要帮忙。
列表点击:
View lastView;
lv_school.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> pa, View v, int pos,long row1) {
if(lastView==null){
v.setBackgroundResource(R.drawable.list_selected);
lastView=v;
}
else{
lastView.setBackgroundColor(Color.WHITE);
v.setBackgroundResource(R.drawable.list_selected);
lastView=v;
}
}
});
我的适配器类是:
public class SchoolAdapterSetting extends BaseAdapter {
private Activity activity;
List<String>data=new ArrayList<String>();
private LayoutInflater inflater = null;
public SchoolAdapterSetting(Activity a, List<String> school_name_List) {
// TODO Auto-generated constructor stub
activity = a;
data = school_name_List;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
vi = inflater.inflate(R.layout.school_item, null);
final TextView info = (TextView) vi.findViewById(R.id.schl_text);
Typeface type1 = Typeface.createFromAsset(getAssets(),"CORBEL.TTF");
info.setTypeface(type1);
if(position==5)
{
info.setBackgroundResource(R.drawable.list_selected);
pressedView=vi;
}
info.setText(data.get(position));
return vi;
}
}