2

我有一个ListViewSimpleCursorAdapter每行包含 3 个不同的TextViews. 我只想用( ) 修改TextViews所有行中的一个,但是它会不断更新每行的所有 3 个。这是我的代码:ViewBinderR.id.text65TextViews

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

4

1 回答 1

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

这样,只有 Currency 字段将被绑定。


此外,您获得强制关闭的原因是,从技术上讲,您的contentView中没有single 而是many。您无法从主布局级别访问它。它仅在单行范围内是唯一的。text65


更新 :

解决方案3:

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

希望能帮助到你。

于 2012-02-22T02:37:45.803 回答