Android ViewBinder只修改所有ListView行中的一项
我有一个Android ViewBinder只修改所有ListView行中的一项,android,android-listview,textview,simplecursoradapter,android-viewbinder,Android,Android Listview,Textview,Simplecursoradapter,Android Viewbinder,我有一个列表视图,它由一个SimpleCursorAdapter填充,每一行包含3个不同的文本视图。我只想使用ViewBinder(R.id.text65)为所有行修改一个textview),但是它会不断更新每行的所有3个textview。这是我的密码: cursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() { public boolean setViewValue(View view, Cursor c
列表视图
,它由一个SimpleCursorAdapter
填充,每一行包含3个不同的文本视图
。我只想使用ViewBinder
(R.id.text65
)为所有行修改一个textview
),但是它会不断更新每行的所有3个textview
。这是我的密码:
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.我尝试了
(TextView)findViewById(R.id.text65)
我得到了一个强制关闭
解决方案1:
您应该检查viewbinder
中的列索引:
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (columnIndex == cursor.getColumnIndexOrThrow(**??**)) // 0 , 1 , 2 ??
{
sign = (TextView) view;
SharedPreferences currency = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String currency1 = currency.getString("Currency", "$");
sign.setText(currency1);
return true;
}
return false;
}
注意,列索引是货币的DBcolumn索引/数据源中列的索引
解决方案2:
您可能正在为列表视图中要绑定的字段定义int[]
,例如:
// and an array of the fields we want to bind those fields to
int[] to = new int[] { R.id.field1, R.id.field2, R.id.Currency };
SimpleCursorAdapter entries = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
。。。有条件地,您可以只传递0
,而不是传递不希望绑定/显示的字段的布局ID
int[] to = new int[] { 0, 0, R.id.Currency };
这样,仅绑定货币字段
另外,之所以要关闭force,是因为从技术上讲,contentView中没有单一的text65
,而是有许多。无法从主布局级别访问它。它仅在单行的范围内是唯一的
更新:
解决方案3:
在ViewBinder
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int viewId = view.getId();
Log.v("ViewBinder", "columnIndex=" + columnIndex + " viewId = " + viewId);
if(viewId == R.id.text65)
{
sign = (TextView) view;
SharedPreferences currency = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String currency1 = currency.getString("Currency", "$");
sign.setText(currency1);
return true;
}
return false;
}
你能试试这个吗
有用提示:您可以使用Log.v
检查代码中的某些值,而无需调试
希望能有所帮助。这都是非常好的信息,谢谢你,不过让我更具体地说明一下情况。在我的列表视图中,每一行都包含一个日期、一个费用和一个货币符号,该费用可以在设置中更改。(例如【6月19日】102.32美元)。如果我的货币符号位于我的数据库中,这些解决方案将非常有效,但它只是位于SharedReferences中的一个简单值,我希望在每一行中复制它。再次感谢您的timeSo所发生的是我的listview行显示为[$$],因为它更改了所有3个文本视图。如果你能解决这个问题,我将不胜感激!隐马尔可夫模型。。您应该能够使用第一个解决方案tho。我不需要游标。getColumnIndexOrThrow(…)
尝试硬编码的值(0、1、2)并做简单的测试,看看是否可以设置货币列。我尝试了数字0-10,但它一直返回false…呃,我希望我很聪明并且知道这件事这很奇怪。。我建议你尝试另一种方法。