【发布时间】:2010-03-30 15:09:37
【问题描述】:
我总是收到 ClassCastException 错误...我不知道该怎么办... - 我正在使用数据投标概念从 sqlite3 数据库中填充列表视图。 - 我只想在长按点击后获取所选项目的文本。
这是活动的代码:
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>
【问题讨论】: