我正在尝试显示来自Cursor
in a ListView
,每行包含一个ImageView
and a TextView
。我有一个CustomCursorAdapter
延伸CursorAdapter
, in 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
}
}
}
The bindView()
方法看起来还可以。
尝试更换你的新视图() method:
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(R.layout.row, parent, false);
}
并且,出于性能原因:
- move
getLayoutInflater()
在里面
构造函数
- 与所有的一样
cursor.getColumnIndexOrThrow()
来电,
作为ened已经说过了
评论
- 使用 StringBuilder 来创建您的lbl text
- 没有必要做Integer.toString(警告阈值)...
只需使用警告阈值
稍后编辑:
你们之间唯一的区别inflate()
方法,我建议的方法是创建与父级匹配的布局参数。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)