当我从ListView中选择时,我遇到了删除数据库条目的问题...我有两个类ViewContentActivity.class
和DBHelper.class
在我的ViewContentActivity.class
中,我将其扩展为ListActivity
并添加方法onListItemClick
我把这段代码,
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
DBHelper helper = new DBHelper(this);
database = helper.getWritableDatabase();
Cursor c = (Cursor)l.getItemAtPosition(position);
int _id = c.getInt(c.getColumnIndex(helper.COL_ID));
String nameList = c.getString(c.getColumnIndex(helper.COL_NAMA));
helper.deleteData(database, _id);
Toast.makeText(this, "Data " + nameList + " removed" , Toast.LENGTH_SHORT).show();
}
在我的DBHelper.class
中,我创建了删除这样的方法..
public void deleteData(SQLiteDatabase db, int id)
{
db.delete(TABLE_PEKERJA, COL_ID + " = " +id, null);
}
当我运行时,我收到错误...错误显示在我声明一个Cursor
之前//Error here
Cursor c = (Cursor)l.getItemAtPosition(position);
int _id = c.getInt(c.getColumnIndex(helper.COL_ID));
String nameList = c.getString(c.getColumnIndex(helper.COL_NAMA));
helper.deleteData(database, _id);
Toast.makeText(this, "Data " + nameList + " removed" , Toast.LENGTH_SHORT).show();
02-09 14:26:01.449: E/AndroidRuntime(13977): at com.example.untukmuna.ViewContentActivity.onListItemClick(ViewContentActivity.java:69)
02-09 14:33:02.419: E/AndroidRuntime(14584): FATAL EXCEPTION: main
02-09 14:33:02.419: E/AndroidRuntime(14584): java.lang.ClassCastException: java.lang.String
02-09 14:33:02.419: E/AndroidRuntime(14584): at com.example.untukmuna.ViewContentActivity.onListItemClick(ViewContentActivity.java:69)
02-09 14:33:02.419: E/AndroidRuntime(14584): at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
02-09 14:33:02.419: E/AndroidRuntime(14584): at android.widget.AdapterView.performItemClick(AdapterView.java:284)02-09 14:33:02.419: E/AndroidRuntime(14584): at android.widget.ListView.performItemClick(ListView.java:3744)
02-09 14:33:02.419: E/AndroidRuntime(14584): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1991)
02-09 14:33:02.419: E/AndroidRuntime(14584): at android.os.Handler.handleCallback(Handler.java:587)
02-09 14:33:02.419: E/AndroidRuntime(14584): at android.os.Handler.dispatchMessage(Handler.java:92)
02-09 14:33:02.419: E/AndroidRuntime(14584): at android.os.Looper.loop(Looper.java:130)
02-09 14:33:02.419: E/AndroidRuntime(14584): at android.app.ActivityThread.main(ActivityThread.java:3687)
02-09 14:33:02.419: E/AndroidRuntime(14584): at java.lang.reflect.Method.invokeNative(Native Method)
02-09 14:33:02.419: E/AndroidRuntime(14584): at java.lang.reflect.Method.invoke(Method.java:507)
02-09 14:33:02.419: E/AndroidRuntime(14584): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
02-09 14:33:02.419: E/AndroidRuntime(14584): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
02-09 14:33:02.419: E/AndroidRuntime(14584): at dalvik.system.NativeStart.main(Native Method)
02-09 14:44:14.639: W/dalvikvm(15238): threadid=1: thread exiting with uncaught exception (group=0x40157560)
02-09 14:44:14.649: E/AndroidRuntime(15238): FATAL EXCEPTION: main
02-09 14:44:14.649: E/AndroidRuntime(15238): java.lang.ClassCastException: java.lang.String
02-09 14:44:14.649: E/AndroidRuntime(15238): at com.example.untukmuna.ViewContentActivity.onListItemClick(ViewContentActivity.java:69)
02-09 14:44:14.649: E/AndroidRuntime(15238): at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
02-09 14:44:14.649: E/AndroidRuntime(15238): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
02-09 14:44:14.649: E/AndroidRuntime(15238): at android.widget.ListView.performItemClick(ListView.java:3744)
02-09 14:44:14.649: E/AndroidRuntime(15238): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1991)
02-09 14:44:14.649: E/AndroidRuntime(15238): at android.os.Handler.handleCallback(Handler.java:587)
02-09 14:44:14.649: E/AndroidRuntime(15238): at android.os.Handler.dispatchMessage(Handler.java:92)
02-09 14:44:14.649: E/AndroidRuntime(15238): at android.os.Looper.loop(Looper.java:130)
02-09 14:44:14.649: E/AndroidRuntime(15238): at android.app.ActivityThread.main(ActivityThread.java:3687)
02-09 14:44:14.649: E/AndroidRuntime(15238): at java.lang.reflect.Method.invokeNative(Native Method)
02-09 14:44:14.649: E/AndroidRuntime(15238): at java.lang.reflect.Method.invoke(Method.java:507)
02-09 14:44:14.649: E/AndroidRuntime(15238): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
02-09 14:44:14.649: E/AndroidRuntime(15238): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
02-09 14:44:14.649: E/AndroidRuntime(15238): at dalvik.system.NativeStart.main(Native Method)
这是我的所有日志消息...我也不知道如何阅读它...只找到任何可以点击的日志....
如何解决这个问题.. ??嗯
答案 0 :(得分:1)
尝试以下代码
db.delete(TableData.DATABASE_TABLE_NAME, "Id=? AND QstnrId=? AND QstnId=?",
new String[] { Id.toString(), QuestionnaireId, QuestionId });
答案 1 :(得分:0)
根据您的异常,我猜你的列表存储字符串,而不是游标。
而不是
Cursor c = (Cursor)l.getItemAtPosition(position);
int _id = c.getInt(c.getColumnIndex(helper.COL_ID));
String nameList = c.getString(c.getColumnIndex(helper.COL_NAMA));
尝试
String nameList = (String) l.getItemAtPosition(position);