[ListView] extrait un TextView
Bonjour à tous,
Voilà une question qui a visiblement été posée souvent mais je n'ai pas su trouver sur le forum un sujet qui puisse m'aider...
J'ai une ListView dont chaque item est composé d'une image et de 2 textes :
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
| <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/bank_picture"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:padding="10px"
android:src="@drawable/bank"
/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="10px"
android:layout_weight="1">
<TextView
android:id="@+id/bank_name"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textStyle="bold"
/>
<TextView
android:id="@+id/bank_address"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
</LinearLayout> |
Je l'utilise ensuite ainsi dans mon code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
MaDataBase bd = new MaDataBase(getBaseContext());
bd.open();
Cursor c = bd.bankList();
if (c.moveToFirst())
do {
map = new HashMap<String,String>();
map.put("bank_name", c.getString(1));
map.put("bank_address", c.getString(2));
listItem.add(map);
} while (c.moveToNext());
bd.close();
liste = (ListView) findViewById(R.id.opList);
SimpleAdapter sa = new SimpleAdapter(this.getBaseContext(), listItem, R.layout.bank_item,
new String[] {"bank_name","bank_address"},
new int[] {R.id.bank_name, R.id.bank_address});
liste.setAdapter(sa);
registerForContextMenu(liste); |
Ce que j'aimerai, c'est pouvoir récupérer, lors d'un clic sur un élément, le contenu du TextView "bank_name" de la ligne en cours :
Code:
1 2 3 4 5
| liste.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String toto = (String) liste.getAdapter().getItem(1);
}
}); |
De ce que j'ai lu il faut créer son propre "adapter" ? Ou existe-t-il d'autres solutions ?
Merci pour vos réponses.