Question

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.

Was it helpful?

Solution

Solution 1:

You should check the column index in the 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;
        }

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 your listview for example :

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

... Conditionally, you can simply pass 0 instead of the layout ids of the fields that you donT want to be bound / shown.

            int[] to = new int[] { 0, 0, R.id.Currency };

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top