Aller au contenu
View in the app

A better way to browse. Learn more.

Forum de Frandroid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Besoin d'aide sur une déclaration de XML

Featured Replies

Posté(e)

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

Posté(e)

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];

Posté(e)
  • Auteur

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?

Modifié par FlorentCode

Posté(e)
  • Auteur

Erreur: The type of the expression must be an array type but it resolved to TypedArray.

Archivé

Ce sujet est désormais archivé et ne peut plus recevoir de nouvelles réponses.

Account

Navigation

Rechercher

Rechercher

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.