[Dev]ListActivity et icones
Bonjour,
J'ai une activité qui dérive de ListActivity. Je voudrais y faire figurer une icone dépendant d'une info stockée en base de données.
Pas de problèmes pour la partie BDD, mais là où je bloque c'est pour faire le lien entre mon info en base (valeurs: 1, 2 ou 3 pour faire simple) et l'image que je veux afficher.
J'ai créé un ViewBinder qui marche niquel pour le texte, mais impossible de récupérer mon ImageView (nullPointerException).
Voilà l'activité (en tout cas l'appel à un simpleCursorAdapter):
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| private void fillData(){
Cursor c = DBHelper.fetchAllRecipes();
startManagingCursor(c);
String[] from = new String[]{RecipeDBAdapter.KEY_NAME};
int[] to = new int[]{R.id.text1};
SimpleCursorAdapter r = new SimpleCursorAdapter(this, R.layout.lig_recettes, c, from, to);
r.setViewBinder(new RecipeBinder());
setListAdapter(r);
} |
et le ViewBinder:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| public class RecipeBinder implements ViewBinder{
public boolean setViewValue(View view, Cursor cursor, int columnIndex){
//ERREUR ICI: NULLPOINTEREXCEPTION
ImageView ty = (ImageView)view.findViewById(R.id.type1);
TextView nom = (TextView)view.findViewById(R.id.text1);
int typeIndex = cursor.getColumnIndex(RecipeDBAdapter.KEY_TYPE);
int t = cursor.getInt(typeIndex);
/*switch(t){
case 1://entrée
type.setImageResource(R.drawable.salad);
break;
case 2://plat
type.setImageResource(R.drawable.hamburger);
break;
/*
case 3://dessert
type.setImageResource(R.drawable.cupcake);
break;
default:
type.setImageResource(R.drawable.salad);
break;
}*/
ty.setImageResource(R.drawable.hamburger);
int nameIndex = cursor.getColumnIndex(RecipeDBAdapter.KEY_NAME);
nom.setText(cursor.getString(nameIndex));
return true;
}
} |
Dans le doute, voilà lig_recettes:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
<?xml version="1.0" encoding="utf-8"?>
<!-- Une recette en ligne -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TableLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="1">
<TableRow>
<ImageView android:id="@+id/type1"
android:layout_width="35.0sp"
android:layout_height="35.0sp"
android:layout_weight="7"/>
<TextView android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="5"
android:textSize="20.0sp"/>
<!--
<CheckBox android:id="@+id/check1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
-->
</TableRow>
</TableLayout>
</LinearLayout> |
Je ne comprend pas pourquoi ça fonctionne avec le texte, mais pas avec l'image..
Si vous pouviez m'éclairer :)
Merci d'avance
@+
Seeme