我通过长按 ListView 项编写了一个带有 SQLite ADD 和 DELETE 功能的简单 APP。但是执行DELETE时有错误。
Logcat(已解决)
04-17 21:19:42.433: E/AndroidRuntime(4067): FATAL EXCEPTION: main
04-17 21:19:42.433: E/AndroidRuntime(4067): java.lang.NullPointerException
04-17 21:19:42.433: E/AndroidRuntime(4067): at tw.danny.idcc_hw2.savedHeadlinesFragment$1.onItemLongClick(savedHeadlinesFragment.java:91)
04-17 21:19:42.433: E/AndroidRuntime(4067): at android.widget.AbsListView.performLongPress(AbsListView.java:2921)
04-17 21:19:42.433: E/AndroidRuntime(4067): at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:2871)
04-17 21:19:42.433: E/AndroidRuntime(4067): at android.os.Handler.handleCallback(Handler.java:615)
04-17 21:19:42.433: E/AndroidRuntime(4067): at android.os.Handler.dispatchMessage(Handler.java:92)
04-17 21:19:42.433: E/AndroidRuntime(4067): at android.os.Looper.loop(Looper.java:137)
04-17 21:19:42.433: E/AndroidRuntime(4067): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-17 21:19:42.433: E/AndroidRuntime(4067): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 21:19:42.433: E/AndroidRuntime(4067): at java.lang.reflect.Method.invoke(Method.java:511)
04-17 21:19:42.433: E/AndroidRuntime(4067): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-17 21:19:42.433: E/AndroidRuntime(4067): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-17 21:19:42.433: E/AndroidRuntime(4067): at dalvik.system.NativeStart.main(Native Method)
我的 APP 是使用 ListView 片段显示数据(标题)和 longClick 项目将其添加到数据库中。在activity2中,以相同的布局显示数据库中的所有数据(标题)。从现在开始,所有功能都运行良好。当我想在activity2中使用与activity1中的ADD相同的操作来实现DELETE时,问题就出现了。
onItemLongClick 监听器(用于删除的活动 2)
@Override
public void onActivityCreated(Bundle savedState) {
super.onActivityCreated(savedState);
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
//Long press to delete article from db
int rnumber = dbHlp.DELETEColumData(RSSData.Headlines.get(position));
if(rnumber==1)
Toast.makeText(getActivity(), "Deleted.", Toast.LENGTH_LONG).show();
else if(rnumber==0)
Toast.makeText(getActivity(), "Delete Error!", Toast.LENGTH_LONG).show();
return true;
}
});
}
onItemLongClick 侦听器(用于添加的活动 1)
@Override
public void onActivityCreated(Bundle savedState) {
super.onActivityCreated(savedState);
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
//Long press to add article to db
long rowID = dbHlp.ADDColumData(RSSData.Headlines.get(position), RSSData.Articles.get(position));
Toast.makeText(getActivity(), "Add to list. id:"+rowID, Toast.LENGTH_LONG).show();
return true;
}
});
}
SQLiteOpenHelper 中的函数
public class RSSDBHlp extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "ReadLater";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "article";
private static final String TABLE_CREATE =
"CREATE TABLE " + TABLE_NAME + " ( " +
" id integer primary key autoincrement, " +
" title TEXT NOT NULL, " +
" detail TEXT );";
public RSSDBHlp(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public long ADDColumData(String Title,String Detail) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values =new ContentValues();
values.put("title", Title);
values.put("detail", Detail);
long rowID =db.insert(TABLE_NAME, null, values);
db.close();
return rowID;
}
public int DELETEColumData(String Title) {
SQLiteDatabase db = getWritableDatabase();
Log.d("MSG","DELETE:"+Title);
String whereClause = "title='" + Title + "'";
int rnumber = db.delete(TABLE_NAME, whereClause, null);
//0:error 1:all row delete
db.close();
return rnumber;
}
有什么问题,我只在 ADD 函数上复制和修改一些代码,效果很好。
删除返回
Returns
the number of rows affected if a whereClause is passed in, 0 otherwise. To remove all rows and get a count pass "1" as the whereClause.