我希望能够根据其类型以不同颜色显示Listview的行。所以我使用下面的代码创建了一个自定义适配器。调用Custom SimpleCursorAdapter时行不刷新
startManagingCursor(cursor);
Lectures_Adapter adapter = new Lectures_Adapter(this,R.layout.menu_item,cursor,FROM,TO);
menuList.setAdapter(adapter);
定制适配器使用此代码 -
public Lectures_Adapter(Context context, int layout, Cursor c, String[] from,int[] to) {
super(context, layout, c, from, to);
this.appContext=context;
this.layout=layout;
this.mycursor=c;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (position > 0)
{
RelativeLayout rowFill = (RelativeLayout) convertView.findViewById(R.id.rowFill);
String title = mycursor.getString(0);
int myColor = 0;
int myPos = title.indexOf("Nurse");
int myPos2 = title.indexOf("masterclass");
if (myPos >= 0)
{
myColor = Color.parseColor("#99FF66");
}
else if (myPos2 >= 0)
{
myColor = Color.parseColor("#FF99FF");
}
else
{
myColor = Color.parseColor("#FFFF66");
}
convertView.findViewById(R.id.rowFill).setBackgroundColor(myColor);
}
return view;
}
这一切工作正常,并显示排在其相应的颜色,但在第一页是始终不变(除了第一行),当我选择一行并返回到Listview,更改不适用。我如何让ListView不断保持自定义适配器设置?
put myListView.invalidateViews();在你的活动方法(UI)中。例如在'myListView.setOnItemClickListener(新OnItemClickListener(){ \t \t \t @Override \t \t \t公共无效onItemClick(适配器视图>父,浏览视图, \t \t \t \t \t INT位置,长ID){ \t \t \t \t myListView.invalidateViews(); \t \t \t} \t \t});' – breceivemail
我编辑了我的答案 – breceivemail