ViewBinder仅修改ListView所有行中的一项
我有一个 ListView
,它由一个 SimpleCursorAdapter
填充,每行包含 3 个不同的 TextView
。我只想使用 ViewBinder
(R.id.text65
) 修改所有行的 TextViews
之一,但它会不断更新所有 3 个每行的 TextViews
。这是我的代码:
cursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
sign = (TextView) view;
SharedPreferences currency = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String currency1 = currency.getString("Currency", "$");
sign.setText(currency1);
return true;
}
});
PS 我尝试了 (TextView) findViewById(R.id.text65);
并且我得到了 强制关闭
。
I have a ListView
that's populated from a SimpleCursorAdapter
each row containing 3 different TextViews
. I only want to modify one of the TextViews
for all rows with a ViewBinder
(R.id.text65
), however it keeps updating all 3 TextViews
of every row. Here's my code:
cursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
sign = (TextView) view;
SharedPreferences currency = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String currency1 = currency.getString("Currency", "$");
sign.setText(currency1);
return true;
}
});
P.S. I tried (TextView) findViewById(R.id.text65);
and I got a Force close
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案1:
您应该检查
viewbinder
中的列索引:注意,列索引是货币的DB列索引/您的任何内容中的列索引数据源。
解决方案 2:
您可能正在为要在
listview
中绑定的字段定义一个int[]
,例如:... 有条件地,您可以简单地传递
0
而不是您不想绑定/显示的字段的布局 ID。这样只有货币字段会被绑定。
另外,您强制关闭的原因是,从技术上讲,您的contentView中没有单个
text65
但很多。您无法从主布局级别访问它。它仅在单行范围内是唯一的。更新:
解决方案 3:
检查
ViewBinder
中视图的id
您可以尝试一下吗?
有用的提示:您可以使用
Log.v
检查代码中的某些值,而无需调试它。希望有帮助。
Solution 1:
You should check the column index in the
viewbinder
:Note, the column index, is the DBcolumn index of the currency / the index of the column in whatever is your data source.
Solution 2:
You are probably defining an
int[]
for the fields to bind to in yourlistview
for example :... Conditionally, you can simply pass
0
instead of the layout ids of the fields that you donT want to be bound / shown.This way only the Currency field will be bound.
Also, the reason you get the force close is because, technically, there is no single
text65
in your contentView but many. You cannot access it from the main layout level. It is unique only in the scope of a single row.Update :
Solution 3:
Check the
id
of the view in theViewBinder
Could you try this?
Useful hint: you can use the
Log.v
to check certain values in your code without having to debug it.Hope it helps.