On clicking on ListView it deletes the value from ListView at index but not deleted that value from that index again.
for eg., it deletes the value from index 1 and again if I delete that value it doesn't delete from index 1. deleted from another index if selected other index
onItemLongClickListener (Book.class)
lview.setOnItemLongClickListener(new
AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View
view, int i, long l) {
String parse = String.valueOf(i);
Integer deletedrows = mydb.DeleteData(String.valueOf(i));
if(deletedrows > 0 )
{
Toast.makeText(BookList.this, "deleted" + parse, Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(BookList.this, "not Deleted" + parse, Toast.LENGTH_SHORT).show();
}
return true;
}
Set data on list view (Book.class)
public void getdata()
{
ListAdapter lviewAdapter;
ArrayList<HashMap<String, String>> userList;
userList = mydb.getalldata();
if(userList.isEmpty())
{
showdata("Error","Nothing Found");
return;
}
ArrayList list = new ArrayList();
StringBuffer buffer = new StringBuffer();
lviewAdapter = new SimpleAdapter(BookList.this, userList,
R.layout.book_custom_list,
new String[]{"ID","url","title"},
new int[]{ R.id.customid,R.id.customurl,R.id.customtitle});
lview.setAdapter(lviewAdapter);
}
getdata method (Database Helper.class)
public ArrayList<HashMap<String, String>> getalldata()
{
SQLiteDatabase db = this.getWritableDatabase();
ArrayList<HashMap<String, String>> userList = new ArrayList<>();
Cursor cursor = db.rawQuery("select * from " + TABLE_NAME,null);
while (cursor.moveToNext()){
HashMap<String,String> user = new HashMap<>();
user.put("ID",cursor.getString(cursor.getColumnIndex(Id_name)));
user.put("title",cursor.getString(cursor.getColumnIndex(title_name)));
user.put("url",cursor.getString(cursor.getColumnIndex(url_name)));
userList.add(user);
}
return userList;
delete data also affected the id showing in the list view, want that to change the id to 1,2,3 in sequential form. thanks
Try this,
lview.setOnItemLongClickListener(new
AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View
view, int i, long l) {
String parse = String.valueOf(i);
//Add this code when you try to delete the item
try{
SQLiteDatabase db = getWritableDatabase();
String query = "delete from TABLE_NAME where id like "+"'"+(ListView_ID)+"'";
db.delete(TABLE_ORDERS, KEY_LISTVIEW_ID + "='" + LISTVIEW_ID+ "'", null);
Cursor cursor = db.rawQuery(query,null);
cursor.close();
//Now set the Data in the ListView from the database after you delete the item
getFreshData();
}
catch (Exception e){
Log.e("order_proof",""+e);
}
return true;
}
private void getFreshData(){
String query = "SELECT * FROM TABLE_NAME";
ArrayList<HashMap<String, String>> userList = new ArrayList<>();
Cursor cursor = db.rawQuery("select * from " + TABLE_NAME,null);
while (cursor.moveToNext()){
HashMap<String,String> user = new HashMap<>();
user.put("ID",cursor.getString(cursor.getColumnIndex(Id_name)));
user.put("title",cursor.getString(cursor.getColumnIndex(title_name)));
user.put("url",cursor.getString(cursor.getColumnIndex(url_name)));
userList.add(user);
}
return userList;
//populate this list in the ListView.
}
Let me know after you add this code @Harpreet.
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.