I can not get the text from a selected item in a listview...pleeeeasss help.
- by Miguel
I always get an ClassCastException error... i do not what else to do...
- I'm using a data biding concept to populated the listview from a sqlite3 database.
- I just want to get the selected item text after a long press click.
This is the code of the activity:
public class ItemConsultaGastos extends ListActivity {
private DataHelper dh ;
TextView seleccion;
private static String[] FROM = {DataHelper.MES, DataHelper.ANO};
private static int[] TO = {R.id.columnaMes, R.id.columnaAno };
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.muestrafechas);
this.dh = new DataHelper(this);
Cursor cursor = dh.selectAllMeses();
startManagingCursor(cursor);
this.mostrarFechas(cursor);
ListView lv = getListView();
lv.setOnItemLongClickListener(new OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int row, long arg3) {
//here is where i got the classCastException.
String[] tmp = (String[]) arg0.getItemAtPosition(row);
//tmp[0] ist the Text of the first TextView displayed by the clicked ListItem
Log.w("Gastos: ", "El texto: " + tmp[0].toString());
return true;
}
});
}
private void mostrarFechas(Cursor cursor) {
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.muestrafechasitem,cursor, FROM, TO);
setListAdapter(adapter);
}
}
///// This is the xml where a define the rows to show on the listview
<TextView
android:id="@+id/espacio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" " />
<TextView
android:id="@+id/columnaAno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_toRightOf="@+id/espacio"/>
<TextView
android:id="@+id/separador1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" -- "
android:layout_toRightOf="@+id/columnaAno"
android:textSize="20sp" />
<TextView
android:id="@+id/columnaMes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/separador1"
android:textSize="20sp"/>