I'm making webview app that contains a bookmarks i successfully added items into listView using Cursor and SQLite database but my problem is i can't get position of item and delete it i tried to do it in my own way but what i got is i had deleted all content of list view. this is my MainActivity, i made a longClickItemListener to delete listview item on long click and this is my BookmarkDatabase
Main Activity
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();
Bookmarks Database
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
}
}