Android ArrayAdapter - ajouter des chaînes dans la liste au lieu de remplacer
-
28-10-2019 - |
Question
Voici comment j'ajoute un tableau à listview:
ListView my_listview = (ListView)findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, my_array);
my_listview.setAdapter(adapter);
my_array est l'URI d'un fichier.
Cela fonctionne bien.Mais la prochaine fois que je veux créer un nouveau tableau, je ne veux pas le remplacer par un nouveau, mais plutôt en ajouter de nouveaux aux existants.Comment puis-je y parvenir?
La solution
Vous devez utiliser un ArrayList<String> my_array
plutôt qu'un String[] my_array
et vous pouvez alors faire:
my_array.addAll(other_array);
pour ajouter des éléments d'un nouveau tableau à votre tableau.Ensuite, vous pouvez faire:
adapter.notifyDataSetChanged();
pour mettre à jour votre ListView avec les nouveaux éléments.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow