【发布时间】:2019-11-01 23:07:15
【问题描述】:
我正在制作包含 书签 的 webview 应用程序,我使用 Cursor 成功地将项目添加到 listView 和SQLite 数据库,但我的问题是我无法获得项目的 位置 并删除它我尝试以自己的方式进行操作,但我得到的是我已经删除了所有内容的列表视图。这是我的 MainActivity,我制作了一个 longClickItemListener 以在长按时删除列表视图项,这是我的 BookmarkDatabase
主要活动
final Dialog bookmarksScreen = new Dialog(this);
bookmarksScreen.requestWindowFeature(Window.FEATURE_NO_TITLE);
bookmarksScreen.setContentView(R.layout.activity_bookmark);
bookmarksScreen.setCancelable(true);
final ListView listView = bookmarksScreen.findViewById(R.id.bookmark_list);
ImageView bookmarkBack = bookmarksScreen.findViewById(R.id.close_bookmarks);
ImageView bookmarkDelete = bookmarksScreen.findViewById(R.id.delete_table);
// populate an ArrayList<String> from the database and then view it
final ArrayList<String> theList = new ArrayList<>();
Cursor data = bookmarkDB.getListContents();
// check if there is no data on database
if(data.getCount() == 0){
Log.d("Database Bookmark", "There is no items on item list");
} else {
while(data.moveToNext()) {
// add data into table "COL1" and "COL2"
theList.add(data.getString(1));
ListAdapter listAdapter = new ArrayAdapter<>(this, R.layout.activity_listitem, theList);
listView.setAdapter(listAdapter);
}
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
webView.loadUrl(item);
bookmarksScreen.dismiss();
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
final int which_item = position;
// here i want to delete listview item
return true;
}
});
bookmarkBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bookmarksScreen.dismiss();
}
});
bookmarkDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showConfirmDialog();
bookmarksScreen.dismiss();
}
});
bookmarksScreen.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
bookmarksScreen.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
bookmarksScreen.show();
书签数据库
package com.rixhion.mint.databases;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class BookmarksDatabase extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "bookmarks.db";
public static final String TABLE_NAME = "bookmarks_data";
public static final String COL1 = "ID";
public static final String COL2 = "ITEM1";
public BookmarksDatabase(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " + " ITEM1 TEXT)";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String item1) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item1);
long result = db.insert(TABLE_NAME, null, contentValues);
//if date as inserted incorrectly it will return -1
if (result == -1) {
return false;
} else {
return true;
}
}
public Cursor getListContents(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
return data;
}
public Cursor deleteSpecificContents() {
SQLiteDatabase db = this.getWritableDatabase();
// Here i want to get the position of item and delete it
}
}
【问题讨论】:
标签: java android android-studio