如何在我的 ListView Cursor Adapter 中更改某些 TextView 的文本颜色
How to change the text color of certain TextViews in my ListView Cursor Adapter
我有一个 ListView
游标适配器加载器。我想根据光标中的值更改 ListView
中某些 TextViews
的文本颜色。问题是 CursorAdapter
正在回收视图,所以我得到了错误的更改。
这是我的光标适配器
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(layout, parent, false);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor){
TextView name_text = (TextView) view.findViewById(R.id.text1);
String name = cursor.getString(cursor.getColumnIndex("name"));
name_text.setText(name);
// below is the problem... the color change is being
// attributed to erroneous textviews in my list.
if (cursor.getInt(cursor.getColumnIndex("favourite"))==1){
name_text.setTextColor(Color.parseColor("#FFFFFF"));
}
}
}
这是我的主
int[] to = new int[] { R.id.text1 };
String[] columns = new String[]{ "name" };
dataAdapter = new CustomAdapter(this, R.layout.text_view, null, columns, to, Adapter.NO_SELECTION);
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
Intent intent = new Intent(getBaseContext(), Recipe.class);
intent.putExtra("ca.ryanklemm.recipebook._id", (int)id);
startActivity(intent);
}
} );
getSupportLoaderManager().initLoader(loaderID, null, this);
在处理适配器内部的视图时,始终在每个 if
语句中使用 else
。
if (cursor.getInt(cursor.getColumnIndex("favourite"))==1){
name_text.setTextColor(Color.parseColor("#FFFFFF"));
}else{
name_text.setTextColor("Default Color");
}
我有一个 ListView
游标适配器加载器。我想根据光标中的值更改 ListView
中某些 TextViews
的文本颜色。问题是 CursorAdapter
正在回收视图,所以我得到了错误的更改。
这是我的光标适配器
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(layout, parent, false);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor){
TextView name_text = (TextView) view.findViewById(R.id.text1);
String name = cursor.getString(cursor.getColumnIndex("name"));
name_text.setText(name);
// below is the problem... the color change is being
// attributed to erroneous textviews in my list.
if (cursor.getInt(cursor.getColumnIndex("favourite"))==1){
name_text.setTextColor(Color.parseColor("#FFFFFF"));
}
}
}
这是我的主
int[] to = new int[] { R.id.text1 };
String[] columns = new String[]{ "name" };
dataAdapter = new CustomAdapter(this, R.layout.text_view, null, columns, to, Adapter.NO_SELECTION);
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
Intent intent = new Intent(getBaseContext(), Recipe.class);
intent.putExtra("ca.ryanklemm.recipebook._id", (int)id);
startActivity(intent);
}
} );
getSupportLoaderManager().initLoader(loaderID, null, this);
在处理适配器内部的视图时,始终在每个 if
语句中使用 else
。
if (cursor.getInt(cursor.getColumnIndex("favourite"))==1){
name_text.setTextColor(Color.parseColor("#FFFFFF"));
}else{
name_text.setTextColor("Default Color");
}