2

我有一个ListViewSimpleCursorAdapter填充,每行包含3个不同的TextViews。我只想修改ViewBinderR.id.text65)的所有行中的一个TextViews,但它不断更新每行的所有3 TextViews。这是我的代码:ViewBinder只修改所有ListView行中的一个项目

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);,我得到了Force close

回答

1

解决方案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[]绑定到你的listview例如:

  // 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 }; 

这样只有货币区域会被绑定。


此外,之所以你会得到部队密切是因为,从技术上讲,没有text65内容查看许多。您无法从主布局级别访问它。它仅在单行的范围内是唯一的。


更新:

解决方案3:

检查视图的idViewBinder

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来检查代码中的某些值,而无需进行调试。

希望它有帮助。

+0

这是所有非常棒的信息,谢谢你,但让我对这种情况稍微具体一点。在我的列表视图中,每行都包含日期,费用和货币符号,并且可以在设置中更改该费用。 (例[6月19日$ 102.32])。如果我的货币符号位于我的数据库中,这些解决方案将非常有效,但它只是一个位于共享首选项中的简单值,我希望每行都重复该值。再次感谢你的时间 – 725623452362 2012-02-22 03:11:21

+0

所以最近发生的事情是我的listview行显示为[$ $ $],因为它改变了所有3个textviews。如果你有这个解决方案,我将不胜感激! – 725623452362 2012-02-22 03:13:33

+0

嗯..你应该可以使用第一个解决方案。不是'cursor.getColumnIndexOrThrow(...)'尝试硬编码的值(0,1,2),并进行简单的测试以查看是否可以获取货币列集。 – 2012-02-22 11:03:27

相关问题