Delete item from database - android

I'm trying to delete an item onLongClick of listview,
but the selected item is not being deleted. Here's my database delete method:
public int deleteReminderEntry(Model id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(REMINDER_TABLE_NAME, REMINDER_COLUMN_ID + " = ?", new String[] {Integer.toString(id.getId())});
}
and my delete action:
model.getId();
database.deleteReminderEntry(model);
reminderListAdapter.notifyDataSetChanged();

You changed the data in the database, not data in the adapter.
After deleting from the database, either fetch the data again and recreate the adapter or delete the item from the ArrayList passed to the adapter.

Related

How to fetch value from sqlite using ID?

I am successfully able to add data to sqlite database,
now when i click on a listview item,
i want to get data from sqlite table ID which is same as listview position
:
My Database looks like this
You need to separately query for getting raw at particular position clicked in LisView.
int id = 0; // id of clicked item
StringBuilder query = new StringBuilder("SELECT * FROM table_name WHERE id = ").append(id);
Cursor cursor = db.rawQuery(query.toString(), null);
if (cursor.moveToFirst()) {
Intent intent = new Intent(context, IntentClass.class);
intent.putExtra("id",id);
intent.putExtra("link",cursor.getString(4));
intent.putExtra("lid",cursor.getString(5));
intent.putExtra("lpass",cursor.getString(6));
intent.putExtra("laddInfo",cursor.getString(7));
context.startActivity(intent);
}
cursor.close();

Show the database last data, first in the listView

when we select data from mysql database and show it in list view, then the 1st data of the database showed in the first of the list.
I want to show the first data in the last of the listView and last data in the first of the list.
Is there any method to do it?
Use this..
SQLiteDatabase sqLiteDatabase=dbHelper.getReadableDatabase();
String query = "select * from " + TABLE_NAME;
Cursor cursor = sqLiteDatabase.rawQuery(query, null);
cursor.moveToLast();
while (cursor.moveToPrevious())
{
//Your code here
.
.
.
}

Populating Gridview and Listview from List created from databaseHelper

I find some tutorials, to create databaseHelper class, which help me to fetch data from database, the data was fetch using List, but somehow i dont know how to populate that list into ArrayList for my gridview and list view, when i try, it says List cannot converted into ArrayList. How can i do that? here my code snippet
databaseHelper.java
public List<Seat> getAllSeats() {
List<Seat> seats = new ArrayList<Seat>();
String selectQuery = "SELECT * FROM " + TABLE_SEATS + "WHERE status=0";
Log.e(LOG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
Seat seat = new Seat();
seat.set_id(c.getInt((c.getColumnIndex("id"))));
seat.set_table_no(c.getString(c.getColumnIndex("table_no")));
// adding to list
seats.add(seat);
} while (c.moveToNext());
}
return seats;
}
index.java
/* LIST SEAT */
final ArrayList<Seat> list_seat = db.getAllSeats();
final GridView gridview_seat = (GridView) findViewById(R.id.gridviewSeat);
gridview_seat.setAdapter(new SeatListAdapter(this, list_seat));
Just convert your list data into array list with following code...
List<Seat> list = db.getAllSeats();
ArrayList<Seat> list_seat = new ArrayList<Seat>(list.size());
list_seat .addAll(list);

Android - SQLite db keeps showing rows that should have been deleted

I'm currently creating an app that downloads lists from a server and places those lists into an SQLite database. In the Mainactivity it calls the lists from the db and places them into a custom adapter, everything works fine and such.
After this I go through several screens to proces the listdata and in the final activity it uses a query to delete the row it's been working on from the db. I use logcat to print the db after this and it shows that the row has been deleted.
Next it takes me back to the Mainactivity and in its onResume I once again load the lists from the db only to find that the row that should have been deleted is still in it. The listview is being updated correctly, it's really an issue of retrieving data from the database that should have been deleted.
So, anybody has an idea why I get rows that have been deleted in another activity?
Mainactivity:
private List<String> list = new ArrayList<>();
public void onResume() {
super.onResume();
context = getApplicationContext();
ConsLoadListDataSource cllDataSource = new ConsLoadListDataSource(context);
cllDataSource.open();
list = cllDataSource.sqliteToListIds();
cllDataSource.close();
logcat("Jadajada: " + list.toString());
}
SQLiteHelper:
public List<String> sqliteToListIds() {
List<String> conList= new ArrayList<>();
if (!db.isOpen()) {
open();
}
Cursor cursor = db.query(ConsLoadListSQLHelper.TABLE_CONS_HEADER,
allConsListColumns, null, null, "loadlist" , null, "loadlist");
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
conList.add(cursor.getString(1));
cursor.moveToNext();
}
db.close();
return conList;
}
The deleting method works, but since it's being asked for:
public void deleteList(int id) {
if (!db.isOpen()) {
open();
}
db.delete(ConsLoadListSQLHelper.TABLE_CONS_HEADER,
ConsLoadListSQLHelper.CONS_HEADER_ID + " = " + id, null);
db.close();
}
edit
I've found a workaround solution by deleting the row on my server and once again retrieving all data from the server before I go back to my MainActivity, but I still don't know how to solve the original problem and this makes my app more dependent on an internet connection, so it's not perfect, but will have to do for now.
For deleting single item you have to use
public void deleteRule(String value) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(Table_Name, Key_value + " = ?",
new String[] { value });
db.close();
}
Hope it helps.

Update listview after update database sqlite

How to update items in listview after update the row on database sqlite. I have put notifyDataSetChanged() but not success, there is not error, but not refresh data in row. The data will change if I click menu back and open list again. So I want update the data in row after I update data. Below structur my code, Thanks.
CustomBaseAdapter adapter;
ListView cListView;
in onCreate:
cListView = (ListView) findViewById(R.id.list_category);
List<ItemsArtikel> rowItems = (List<ItemsArtikel>) db.getAllCategory(katname);
adapter = new CustomBaseAdapter(this, rowItems);
cListView.setAdapter(adapter);
Query listcategory:
public List<ItemsArtikel> getAllCategory(String cat) {
SQLiteDatabase db = this.getWritableDatabase();
List<ItemsArtikel> cList = new ArrayList<ItemsArtikel>();
String selectQuery = "SELECT * FROM " + TABLE_CAT + " WHERE "
+ COLOM_KAT + "=\"" + cat + "\"";
Cursor c = db.rawQuery(selectQuery, null);
c.moveToFirst();
if (c.getCount() > 0) {
do {
ItemsArtikel kat = new ItemsArtikel();
kat.setID(c.getLong(c.getColumnIndex(COLOM_ID)));
kat.setName(c.getString(c.getColumnIndex(COLOM_NAME)));
kat.setLink(c.getString(c.getColumnIndex(COLOM_LINK)));
kat.setImage(c.getString(c.getColumnIndex(COLOM_IMAGE)));
kategoriList.add(kat);
} while (c.moveToNext());
}
return cList;
}
After i use for listview is presented for to download images and update each row the database using AsyncTask when download image. Image success downloaded and row also success updated, Just not refresh in listview.
This is code onPostExecute when download image:
#Override
protected void onPostExecute(HashMap<String, Object> result) {
String id = (String) result.get("id");
String path = (String) result.get("image");
// update row in database
db.updateRowCategory(id, path);
cListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
On calling notifyDataSetChanged() it will just notify the register view to update the content to reflect the changes.
In your case you are updating data in Database not in Adapter Data model. To reflect the changes in ListView you need to updated the Adapter's datamodel and then call notifyDataSetChanged();
OR
If your are rendering data directly from Database use CursorAdapter and implement onContentChanged () for updating ListView
Use following line before add adapter.
#Override
protected void onPostExecute(HashMap<String, Object> result) {
String id = (String) result.get("id");
String path = (String) result.get("image");
// update row in database
db.updateRowCategory(id, path);
cListView.Invalidate();
cListView.setAdapter(adapter);
cListView.Invalidate();
adapter.notifyDataSetChanged();
}

Categories

Resources