nella mia app Android ho creato un componente ListView chiamato myList, e riempito con oggetti della mia tipo personalizzato:Come ottenere l'oggetto selezionato da ListView?
class MyClass{
private String displayName;
private String theValue;
... //here constructor, getters, setters and toString() are implemented
}
ho usato l'ArrayAdapter per legava i theObjects ArrayList con myList:
ArrayAdapter<MyClass> adapter=
new ArrayAdapter<MyClass>(this, R.layout.lay_item, theObjects);
myList.setAdapter(adapter);
Funziona bene, l'elenco è popolato e così via, ma quando sto cercando di accedere all'elemento selezionato, ricevo un oggetto Null. Ho fatto questo usando
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
MyClass selItem = (MyClass) myList.getSelectedItem(); //
String value= selItem.getTheValue(); //getter method
}
Quale sembra essere il problema? Grazie
Ha detto che può visualizzare la roba, quindi la classe deve aver istanziato. – xandy