2

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.

user
  • 86,916
  • 18
  • 197
  • 190
725623452362
  • 115
  • 2
  • 13

1 Answers1

1

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.

Orkun
  • 6,998
  • 8
  • 56
  • 103
  • This is all very great information thank you, however let me be a little more specific about the situation. In my listview, each row contains a date, an expense, and a currency symbol with that expense that can be changed in the settings. (Ex.[June 19 $102.32]). These solutions would work great if my currency symbol was located in my database, but its just a simple value located in sharedpreferences that I want duplicated in every row. Thank you again for your time – 725623452362 Feb 22 '12 at 03:11
  • So whats happening is my listview rows show up as [$ $ $] because its changing all 3 of the textviews. If you have a solution for this I would be grateful! – 725623452362 Feb 22 '12 at 03:13
  • hmm.. You should be able to use the first solution tho. isntead of `cursor.getColumnIndexOrThrow(...)` try hardcoded values (0, 1, 2) and do simple tests to see if you can get the currency column set. – Orkun Feb 22 '12 at 11:03
  • I tried numbers 0-10 but it kept returning false....ugh I wish I was smart and knew about this stuff – 725623452362 Feb 22 '12 at 15:00
  • it s weird.. I ve suggested another approach for you to try. – Orkun Feb 22 '12 at 15:19