[英]SimpleCursorAdapter.ViewBinder - Apply differently to each row (ListFragment)
In my app, I pull 3 strings from an SQlite database to populate a ListView. 在我的应用程序中,我从SQlite数据库中提取了3个字符串以填充ListView。 In the listrow I have to TextViews and an ImageView.
在列表行中,我必须有TextViews和ImageView。
The two TextViews are currently populated properly(with different text in each row). 当前正确填充了两个TextView(每行中有不同的文本)。 But, this is not the case for the ImageView, which I set using a ViewBinder.
但是,我使用ViewBinder设置的ImageView并非如此。 Namely, the image in each row all get the same color.
即,每一行中的图像都获得相同的颜色。 How can i fix this?
我怎样才能解决这个问题? Do I need to extend SimpleCursorAdapter or something?
我需要扩展SimpleCursorAdapter之类的东西吗?
String[] from = {DbHelper.NAME, DbHelper.COMMENT, DbHelper.COLOR};
String[] column = {DbHelper.C_ID, DbHelper.NAME, DbHelper.COMMENT, DbHelper.COLOR};
int[] to = {R.id.listitem_title, R.id.listitem_summary, R.id.listitem_color};
Cursor cursor = db.query(DbHelper.TABLE_NAME, column, null, null, null, null, null);
this.adapter = new SimpleCursorAdapter(getActivity(), R.layout.list_item_set, cursor, from, to);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (view.getId() == R.id.listitem_color) {
cursor.moveToFirst();
String color = "#ffffff";
while (cursor.moveToNext()) {
color = cursor.getString(cursor.getColumnIndex(DbHelper.COLOR));
}
((ImageView) view).setBackgroundColor(Color.parseColor(color));
return true;
}
return false;
}
});
setListAdapter(adapter);
Namely, the image in each row all get the same color.
即,每一行中的图像都获得相同的颜色。 How can i fix this?
我怎样才能解决这个问题?
If you're going to use: 如果您要使用:
String color = "#ffffff";
while (cursor.moveToNext()) {
color = cursor.getString(cursor.getColumnIndex(DbHelper.COLOR));
}
this will make the color
point to the color present in the last row of the Cursor
as you loop through the entire Cursor
. 当您遍历整个
Cursor
这将使color
指向Cursor
最后一行中存在的颜色。 Instead you'll want to simply get the color
from the Cursor
: 相反,您只需要从
Cursor
获取color
:
String color = "#ffffff";
color = cursor.getString(cursor.getColumnIndex(DbHelper.COLOR));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.