Ajouter une valeur à une ListView
Bonjour à tous,
Dans mon application Android, j'ai une activité qui contient un bouton permettent de créer de nouveaux profils. Voilà le layout de l'activité :
Fichier creation.xml
Code:
1 2 3 4 5 6
|
<LinearLayout>
<TextView/>
<Button/>
<ListView> <ListView/>
</LinearLayout> |
La ListView contient la liste des profils créés.
En cliquant sur le bouton (Button), une boite de dialogue se lance (AlertDialog), ayant ce layout :
Fichier profil.xml
Code:
1 2 3 4 5
|
<LinearLayout>
<TextView/>
<EditText/>
</LinearLayout> |
Cette boite de dialogue permet de créer un nouveau profil. on valide en cliquant sur le bouton "Enregistrer" (PositiveButton) ou on annule en cliquant sur le bouton "Annuler" (NegativeButton).
La validation devrait ajouter le profil créé à la ListView. C'est ici que mon problème se pose.
Voilà le code de création :
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
private Button creation;
private Intent intent;
private ListView profilList, list;
private ArrayAdapter<String> listadapter;
private EditText edit;
private String[] values;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.creation_profil);
edit = (EditText) findViewById(R.id.NameProfil);
creation = (Button) findViewById(R.id.creation);
creation.setOnClickListener(this);
list = creerListView();
}
@Override
public void onClick(View v) {
if (v == creation){
afficherBoiteProfil();
}
}
private ListView creerListView(){
profilList = (ListView) findViewById(R.id.profilList);
values = new ArrayList();
listadapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
profilList.setAdapter(listadapter);
profilList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
return profilList;
}
private void afficherBoiteProfil(){
//On instancie notre layout en tant que View
LayoutInflater factory = LayoutInflater.from(this);
final View alertDialogView = factory.inflate(R.layout.creation, null);
//creation de l'alerteDialog
AlertDialog.Builder createProfilDialog = new AlertDialog.Builder(this);
createProfilDialog.setView(alertDialogView);
createProfilDialog.setTitle("Création d'un profil");
createProfilDialog.setIcon(R.drawable.add_user32);
createProfilDialog.setPositiveButton("Enregistrer",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String value = edit.getText().toString();
listadapter.add(value);
intent = new Intent(getApplicationContext(), CreationProfil.class);
startActivity(intent);
}
});
createProfilDialog.setNegativeButton("Annuler", null);
createProfilDialog.show();
} |
Le contenu de ma Liste est défini par le tableau de String "values".
En cliquant sur le bouton "Enregistrer", l'application se plante!
Merci d'avance pour votre aide.