FlorentCode Posted August 25, 2010 Share Posted August 25, 2010 Bonsoir, Aprés des recherches infructueuses, je viens vous demander conseille sur mon probleme: J'utilise un AlertDialog qui comporte une liste (singlechoice->radioButtons). Lorsque je choisis l'item dans ma liste, un toast s'affiche avec mon item et je fais un setText de l'item sur un textView. Tout marche quand je déclare mes items comme cela: -------------------------------------------------------------------------------------------------------- ((ImageButton)findViewById(R.id.pickPays)).setOnClickListener(new OnClickListener(){ public void onClick(View v) { final CharSequence[] items = {"United States", "France", "Sweden"}; AlertDialog.Builder builder = new AlertDialog.Builder(Postit.this); builder.setTitle("Selectionne ton pays"); builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { dialog.cancel(); Toast.makeText(getApplicationContext(), items[item] , Toast.LENGTH_LONG).show(); ((TextView) findViewById(R.id.paysDisplay)).setText(items[item]); } }); AlertDialog alert = builder.create(); alert.show(); } }); -------------------------------------------------------------------------------------------------------- Mais je voudrais déclarer mes items dans un array.xml J'ai donc crée un array.xml 'pays.xml' et j'ai changé mon code comme ci-dessous. Le probleme est que l'application plante lors de la selection de l'item cad au moment de l'affichage du toast et du setText. Je n'arrive pas a regler le probleme qui parait simple? -------------------------------------------------------------------------------------------------------- ((ImageButton)findViewById(R.id.pickPays)).setOnClickListener(new OnClickListener(){ public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(Postit.this); builder.setTitle("Selectionne ton pays"); builder.setSingleChoiceItems(R.array.pays, -1, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { dialog.cancel(); Toast.makeText(getApplicationContext(), item , Toast.LENGTH_LONG).show(); ((TextView) findViewById(R.id.paysDisplay)).setText(item); } }); AlertDialog alert = builder.create(); alert.show(); } }); ---------------------------------------------------------------------------------------- Merci d'avance, Florent Link to comment Share on other sites More sharing options...
EricLarch Posted August 28, 2010 Share Posted August 28, 2010 Je ne suis pas sur car il n'y a pas ton stack trace ni ta déclaration XML mais le problème provient probablement de Toast.makeText(getApplicationContex(), item, Toast.LENGTH_LONG) A priori item correspond à l'index de l'item qui est cliqué dans le tableau, et non pas à l'id de la resource. Pour récupérer le text de l'item tu dois faire comme suit: Resources res = getResources(); TypedArray items = res.obtainTypedArray(R.array.pays); String text = items[item]; Link to comment Share on other sites More sharing options...
FlorentCode Posted August 28, 2010 Author Share Posted August 28, 2010 Merci pour ton message. J'essaie toujours de le déclarer dans un array.xml en vain. J'ai essayé comme tu m'as conseillé mais j'ai une erreur: ((ImageButton)findViewById(R.id.pickPays)).setOnClickListener(new OnClickListener(){ public void onClick(View v) { //final CharSequence[] items = {"Angleterre","Danemark","Espagne","Finland","France","Norvege", "Suede" }; AlertDialog.Builder builder = new AlertDialog.Builder(Profil.this); builder.setTitle("Selectionne ton pays"); builder.setSingleChoiceItems(R.array.pays, -1, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { Resources res = getResources(); TypedArray items = res.obtainTypedArray(R.array.pays); String text = items[item]; dialog.cancel(); Toast.makeText(getApplicationContext(), "Server request: " + text , Toast.LENGTH_LONG).show(); ((TextView) findViewById(R.id.paysDisplay)).setText(text); } }); AlertDialog alert = builder.create(); alert.show(); } }); Une idée? Link to comment Share on other sites More sharing options...
EricLarch Posted August 28, 2010 Share Posted August 28, 2010 tu peux mettre l'erreur complète que tu as? Link to comment Share on other sites More sharing options...
FlorentCode Posted August 29, 2010 Author Share Posted August 29, 2010 Erreur: The type of the expression must be an array type but it resolved to TypedArray. Link to comment Share on other sites More sharing options...
EricLarch Posted August 29, 2010 Share Posted August 29, 2010 Essaye String text = items.getString(item) Link to comment Share on other sites More sharing options...
FlorentCode Posted August 29, 2010 Author Share Posted August 29, 2010 Merci !! ;) Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.