Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set Image source to a listView depending on database fill data
    text
    copied!<p>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:</p> <pre><code>public class teamsListFragment extends ListFragment implements LoaderManager.LoaderCallbacks&lt;Cursor&gt; { 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; } }; } </code></pre> <p>This is my teamrow.xml</p> <pre><code>&lt;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" /&gt; &lt;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" /&gt; </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload