我有一個ListActivity,其中顯示了一些條目。一旦選擇了一個條目,我將顯示另一個具有該條目內容的活動。當我回到ListActivity時,我希望未讀條目具有與讀條目不同的顏色。更改ListView中所選條目的背景顏色
我有一個數據庫存儲的條目,當我選擇一個數據庫字段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();
}
}
嘗試在NewView的使用c.getLong(readCol)= 0,而不是讀= 0時,所有我想說的是,避免新變量。這是造成問題的原因。 –
它不會改變ListView行爲,我想補充一點,它看起來像背景顏色以週期性的方式變化(例如每5個條目)。 – flyhalf8610