如何重写 CursorAdapterbindView
我正在尝试在 ListView
中显示来自 Cursor
的信息,每行包含一个 ImageView
和一个 TextView
。我有一个 CustomCursorAdapter
扩展 CursorAdapter
,在 bindView
中,我评估来自光标的数据,并基于该数据设置视图图像和文本。
当我运行应用程序时,ListView
显示正确的行数,但它们是空的。我知道我在重写 bindView 时错过了一些东西,但我不确定是什么。
任何帮助将不胜感激。
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
}
}
}
I'm trying to display information from a Cursor
in a ListView
, each row contains a ImageView
and a 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.
When I run the app the ListView
displayes the correct amount of rows, but they are empty. I know I have missed something when overriding bindView, but I'm not sure what.
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
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
bindView()
方法看起来没问题。尝试替换您的 newView() 方法:
并且,出于性能原因:
getLayoutInflater()
移至构造函数
cursor.getColumnIndexOrThrow()
调用相同,正如 已完成 中已经说过的
注释
只需使用warningThreshold
稍后编辑:
您的
inflate()
方法与我建议的方法之间的唯一区别是,该方法创建与父级匹配的布局参数。The
bindView()
method seems ok.Try replacing your newView() method:
And, for performance reasons:
getLayoutInflater()
in theconstructor
cursor.getColumnIndexOrThrow()
calls,as ened already said in the
comments
just use warningThreshold
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.