[英]How to override CursorAdapter bindView
I'm trying to display information from a Cursor
in a ListView
, each row contains a ImageView
and a TextView
. 我正在尝试从
ListView
的Cursor
显示信息,每行包含一个ImageView
和一个TextView
。 I have a CustomCursorAdapter
extending CursorAdapter
, in bindView
I evaluate the data from the cursor and based on that set the views image and text. 我有一个
CustomCursorAdapter
扩展CursorAdapter
,在bindView
我评估光标中的数据并根据它设置视图图像和文本。
When I run the app the ListView
displayes the correct amount of rows, but they are empty. 当我运行应用程序时,
ListView
显示正确的行数,但它们是空的。 I know I have missed something when overriding bindView, but I'm not sure what. 我知道我在覆盖bindView时遗漏了一些东西,但我不确定是什么。
Any help would be greatly appreciated. 任何帮助将不胜感激。
private class CustomCursorAdapter extends CursorAdapter {
public CustomCursorAdapter() {
super(Lmw.this, monitorsCursor);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater layoutInflater = getLayoutInflater();
return layoutInflater.inflate(R.layout.row, null);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
try {
int monitorNameIndex = cursor.getColumnIndexOrThrow(DbAdapter.MONITORS_COLUMN_MONITOR_NAME);
int resultTotalResponseTimeIndex = cursor.getColumnIndexOrThrow(DbAdapter.RESULTS_COLUMN_TOTAL_RESPONSE_TIME);
String monitorName = cursor.getString(monitorNameIndex);
int warningThreshold = cursor.getInt(resultTotalResponseTimeIndex);
String lbl = monitorName + "\n" + Integer.toString(warningThreshold) + " ms";
TextView label = (TextView) view.findViewById(R.id.label);
label.setText(lbl);
ImageView icon = (ImageView)view.findViewById(R.id.icon);
if(warningThreshold < 1000) {
icon.setImageResource(R.drawable.ok);
} else {
icon.setImageResource(R.drawable.alarm);
}
} catch (IllegalArgumentException e) {
// TODO: handle exception
}
}
}
The bindView()
method seems ok. bindView()
方法似乎没问题。
Try replacing your newView() method: 尝试替换newView()方法:
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(R.layout.row, parent, false);
}
And, for performance reasons: 并且,出于性能原因:
getLayoutInflater()
in the constructor getLayoutInflater()
cursor.getColumnIndexOrThrow()
calls, as ened already said in the cursor.getColumnIndexOrThrow()
调用,如已经说过ened Later edit : The only difference between your inflate()
method and the one I suggested is that this one creates layout params that match the parent. 稍后编辑 :你的
inflate()
方法与我建议的方法之间的唯一区别是,这个方法创建了与父级匹配的布局参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.