I am trying to delete an item from a listview via a dialog menu. When an item is selected, I retrive the item's name and give an option to delete that item given the item's name. But when I try to delete and reload the listview (see picture). I get errors on SQL statements.
picture: http://i.imgur.com/zkmadGf.png
Here is the deleting method:
termList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
final String termName = ((TextView) view.findViewById(R.id.tv_termName)).getText().toString();
builder.show();
builder.setAdapter(menAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String term = termName;
switch (which) {
case 0:
Context c = MainActivity.this;
Intent i = new Intent(c, CourseListActivity.class);
i.putExtra("termName", term);
c.startActivity(i);
break;
case 1:
db.delete("termTable", "name = " + term, null);
termListAdapter.notifyDataSetChanged();
}
}
});
}
});
Here is the log file:
04-08 14:27:00.190: E/AndroidRuntime(26451): FATAL EXCEPTION: main
04-08 14:27:00.190: E/AndroidRuntime(26451): android.database.sqlite.SQLiteException: near "Name": syntax error: , while compiling: DELETE FROM termTable WHERE name = Term Name: hello
04-08 14:27:00.190: E/AndroidRuntime(26451): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
04-08 14:27:00.190: E/AndroidRuntime(26451): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
04-08 14:27:00.190: E/AndroidRuntime(26451): at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
04-08 14:27:00.190: E/AndroidRuntime(26451): at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
04-08 14:27:00.190: E/AndroidRuntime(26451): at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
04-08 14:27:00.190: E/AndroidRuntime(26451): at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:84)
04-08 14:27:00.190: E/AndroidRuntime(26451): at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1759)
04-08 14:27:00.190: E/AndroidRuntime(26451): at com.dagonar.achievegpa.MainActivity$2$1.onClick(MainActivity.java:89)
04-08 14:27:00.190: E/AndroidRuntime(26451): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:938)
04-08 14:27:00.190: E/AndroidRuntime(26451): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
04-08 14:27:00.190: E/AndroidRuntime(26451): at android.widget.AbsListView.performItemClick(AbsListView.java:1485)
04-08 14:27:00.190: E/AndroidRuntime(26451): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2941)
04-08 14:27:00.190: E/AndroidRuntime(26451): at android.widget.AbsListView$1.run(AbsListView.java:3658)
04-08 14:27:00.190: E/AndroidRuntime(26451): at android.os.Handler.handleCallback(Handler.java:605)
04-08 14:27:00.190: E/AndroidRuntime(26451): at android.os.Handler.dispatchMessage(Handler.java:92)
04-08 14:27:00.190: E/AndroidRuntime(26451): at android.os.Looper.loop(Looper.java:137)
04-08 14:27:00.190: E/AndroidRuntime(26451): at android.app.ActivityThread.main(ActivityThread.java:4464)
I hope you could help me find a solution.
Probably your goal is to execute the following statement:
DELETE FROM termTable WHERE name = 'hello'
instead of:
DELETE FROM termTable WHERE name = Term Name: hello
So the correct code will be:
String term = termName.split(":")[1].trim();
switch (which) {
case 0:
Context c = MainActivity.this;
Intent i = new Intent(c, CourseListActivity.class);
i.putExtra("termName", term);
c.startActivity(i);
break;
case 1:
db.delete("termTable", "name = '" + term + "'", null);
termListAdapter.notifyDataSetChanged();
}
And please split label and value into the 2 TextViews
, don't put everything inside the one TextView
.
将术语值视为“术语名称:hello”,您需要的是db.delete(“ termTable”,“ name =” +“'+ term +'”,null);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.