我有一个 ListActivity,我在其中显示了一些条目。选择条目后,我会显示另一个包含条目内容的 Activity 。我希望,当我返回到 ListActivity 时,未读条目的颜色与已读条目的颜色不同。
我有一个存储条目的数据库,当我选择一个条目时,数据库字段 COLUMN_READ 会更新。
我有一个自定义的 ListAdapter:
public class CustomListAdapter extends SimpleCursorAdapter{
private Context context;
private int layout;
public CustomListAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.context = context;
this.layout = layout;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Cursor c = getCursor();
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);
int titleCol = c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_TITLE);
int authorCol=c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_AUTHOR_NAME);
int readCol= c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_READ);
String title = c.getString(titleCol);
String author = c.getString(authorCol);
long read= c.getLong(readCol);
TextView name_text = (TextView) v.findViewById(R.id.title);
if (name_text != null) {
name_text.setText(title);
}
TextView content_text = (TextView) v.findViewById(R.id.subtitle);
if (content_text != null) {
content_text.setText(author);
}
if (read!=0){
name_text.setBackgroundColor(Color.GRAY);
}
return v;
}
@Override
public void bindView(View v, Context context, Cursor c) {
int titleCol = c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_TITLE);
int authorCol=c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_AUTHOR_NAME);
int readCol= c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_READ);
String title = c.getString(titleCol);
String author = c.getString(authorCol);
long read= c.getLong(readCol);
TextView name_text = (TextView) v.findViewById(R.id.title);
if (name_text != null) {
name_text.setText(title);
}
TextView content_text = (TextView) v.findViewById(R.id.subtitle);
if (content_text != null) {
content_text.setText(author);
}
if (read!=0){
name_text.setBackgroundColor(Color.GRAY);
}
}
}
我所做的是:
CustomListAdapter present = new CustomListAdapter(context,
R.layout.atom_list_row, loadingCursor, new String[] {DataBaseSchema.EntrySchema.COLUMN_TITLE,DataBaseSchema.EntrySchema.COLUMN_AUTHOR_NAME, DataBaseSchema.EntrySchema.COLUMN_READ}, new int[]{R.id.title,R.id.subtitle,R.id.row_container});
setListAdapter(present);
问题是突出显示的条目没有正确显示,我不明白为什么。实际上,所选条目的背景颜色会发生变化,而且其他一些条目的背景颜色似乎是随机的,而且当我滚动其他条目时也会改变它们的颜色。 这是一个Android错误吗?是否有一些解决方法,或者我遗漏了什么?
提前致谢..
编辑
我找到了解决方案!!!!
我在代码末尾添加了 v.refreshDrawableState();
。
此代码有效:
public class CustomListAdapter extends SimpleCursorAdapter{
private Context context;
private int layout;
public CustomListAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.context = context;
this.layout = layout;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Cursor c = getCursor();
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);
int titleCol = c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_TITLE);
int authorCol=c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_AUTHOR_NAME);
int readCol= c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_READ);
String title = c.getString(titleCol);
String author = c.getString(authorCol);
long read= c.getLong(readCol);
TextView name_text = (TextView) v.findViewById(R.id.title);
if (name_text != null) {
name_text.setText(title);
}
TextView content_text = (TextView) v.findViewById(R.id.subtitle);
if (content_text != null) {
content_text.setText(author);
}
if (read!=0){
v.setBackgroundColor(Color.GRAY);
}
else v.setBackgroundColor(Color.BLACK);
v.refreshDrawableState();
return v;
}
@Override
public void bindView(View v, Context context, Cursor c) {
int titleCol = c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_TITLE);
int authorCol=c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_AUTHOR_NAME);
int readCol= c.getColumnIndex(DataBaseSchema.EntrySchema.COLUMN_READ);
String title = c.getString(titleCol);
String author = c.getString(authorCol);
//long read= c.getLong(readCol);
TextView name_text = (TextView) v.findViewById(R.id.title);
if (name_text != null) {
name_text.setText(title);
}
TextView content_text = (TextView) v.findViewById(R.id.subtitle);
if (content_text != null) {
content_text.setText(author);
}
//LinearLayout linear =(LinearLayout) v.findViewById(R.id.row_container);
if (c.getLong(readCol)!=0){
v.setBackgroundColor(Color.GRAY);
}else v.setBackgroundColor(Color.BLACK);
v.refreshDrawableState();
}
}
最佳答案
读到这里,你会有一个想法Colored item in listview
关于android - 仅更改 ListView 中选定条目的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7684517/