views:

117

answers:

1

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:

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:padding="10sp">

       <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"/> 

</RelativeLayout>
+1  A: 

Change:

String[] tmp = (String[]) arg0.getItemAtPosition(row);

to:

Object tmp = arg0.getItemAtPosition(row);

Set a breakpoint on the line where you are getting the ClassCastException. Start the application in debug mode (In Eclipse right click the project and select Debug as->Android Application). When you reach the breakpoint in your code inspect tmp to see what it actually is. It's obviously not a String[] like you are expecting it to be.

mbaird
It is going to be a `Cursor`, pointing to the data at the indicated row.
CommonsWare