2

3つの異なるを含む各行ListViewから入力されたがあります。すべての行の1つを( )で変更したいのですが、すべての行の3つすべてが更新され続けます。これが私のコードです:SimpleCursorAdapterTextViewsTextViewsViewBinderR.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:解決策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に単一 ではなく、多数あるためです。メインレイアウトレベルからはアクセスできません。これは、単一行のスコープでのみ一意です。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 に答える