2010-03-30 39 views
0

我總是得到一個ClassCastException錯誤...我不知道該怎麼辦... - 我使用數據標識概念從sqlite3數據庫填充listview。 - 我只想在長按一下鼠標後獲得選定的項目文字。我無法從列表視圖中的選定項目獲取文本... pleeeeasss幫助

這是活動的代碼:

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); 
} 

} 

這是XML其中限定的行,以顯示在列表視圖:

<?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> 

回答

3

變化:

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

到:

Object tmp = arg0.getItemAtPosition(row); 

在得到ClassCastException的行上設置一個斷點。以調試模式啓動應用程序(在Eclipse中,右鍵單擊該項目並選擇Debug as-> Android Application)。當您到達代碼中的斷點時,請檢查tmp以查看它的實際內容。這顯然不像你期待的那樣是String[]

+2

它將是一個'Cursor',指向指定行的數據。 – CommonsWare 2010-03-30 16:29:48

相關問題