0

Basically what i'm trying to do is set dynamically a image source to a image view depending on the id that i get from a precreated database. this is my ListFragment:

public class teamsListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
    String[] teams;
    private SimpleCursorAdapter adapter; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] projection = {DBAdapter.KEY_ROWID, DBAdapter.KEY_NAME};
        String[] uiBindFrom = {DBAdapter.KEY_NAME};
        int[] uiBindTo = {R.id.label};

        getLoaderManager().initLoader(0, null, this);
        adapter = new SimpleCursorAdapter(
                getActivity().getApplicationContext(), R.layout.team_row,
                null, uiBindFrom, uiBindTo, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        adapter.setViewBinder(VIEW_BINDER);
        setListAdapter(adapter);
    }
    static final SimpleCursorAdapter.ViewBinder VIEW_BINDER = new SimpleCursorAdapter.ViewBinder() {
        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            if(view.getId() != R.id.icon) return false;
            ImageView teamIcon = (ImageView) view;
            int idTeam = cursor.getInt(cursor.getColumnIndex(DBAdapter.KEY_ROWID));
            switch(idTeam) {
            case 1:
                teamIcon.setImageResource(R.drawable.team1);
                break;
            case 2:
                teamIcon.setImageResource(R.drawable.team2);
                break;
            }
            return true;
        }
    };
}

This is my teamrow.xml

<ImageView
    android:id="@+id/icon"
    android:layout_width="30dp"
    android:layout_height="24dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
     />

<TextView
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="6dp"
    android:lines="1"
    android:text="@+id/TextView01"
    android:textSize="24dp" />

The problem is that it doesn't work the image source is not set. I think is the view binder but I'm new with android and I really appreciate if could show me my error or if there is a better way to achieve this. Thanks in advance.

Sam
  • 86,580
  • 20
  • 181
  • 179
Henry1988
  • 83
  • 1
  • 1
  • 8
  • why are you using `static final` to declare you binder? – Moog Jul 13 '12 at 16:11
  • @Merlin i did it this way because i was following this this tutorial [link](http://www.youtube.com/watch?v=qVz4vhhEces&list=PLE08A97D36D5A255F&index=23&feature=plpp_video) and they do it on this way. – Henry1988 Jul 13 '12 at 18:43
  • I noticed this in another book ... I was curious as to the technical reason ... I probably need to revisit Android optimizations. – Moog Jul 14 '12 at 01:27

0 Answers0