lundi 24 août 2015

Delete from SQLite and Update Listview with SimpleCursorAdapter

How can i delete an item from a listView which is backed by SQLite and update the listView with the new contents?

I've tried to follow solutions to other examples, but can't seem to remove the items.

Class which shows data in listView:

public class MyFeedsScreen extends ListActivity {

DatabaseHandler dbHandler;
SimpleCursorAdapter dataAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {

    //Set the layout of MyFeeds screen
    super.onCreate(savedInstanceState);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.my_feeds_screen);


    dbHandler = new DatabaseHandler(MyFeedsScreen.this);
    displayList();
}

public void displayList() {

    //Call method to open database
    dbHandler.getWritableDatabase();

    //Open database and get instance of database handler
    dbHandler = new DatabaseHandler(this);

    final Cursor cursor = dbHandler.getData();
    //Takes string array for title, link and description and maps them different textView items
    String from[] = new String[]{dbHandler.columnTitle, dbHandler.link, dbHandler.description};
    int to[] = new int[]{R.id.textView1};
    dataAdapter = new SimpleCursorAdapter(this, R.layout.row_item, cursor, from, to, 0);

    final ListView lv = getListView();
    lv.setAdapter(dataAdapter);


    lv.setLongClickable(true);
    lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {


            try {
                dbHandler.open();
            } catch (SQLException e) {
                e.printStackTrace();
            }

            final String itemId = cursor.getString(cursor.getColumnIndex("_id"));

            dbHandler.deleteItemFromTable(itemId);
            dbHandler.getData();

            dataAdapter.notifyDataSetChanged();
            displayList();

            Toast.makeText(MyFeedsScreen.this, "Item has been deleted from MyFeeds!", Toast.LENGTH_SHORT).show();


            return true;
        }
    });
}//displayList()}//class

DatabaseHandler.java - methods to populate listview and delete items

  //get data from database for listview
public Cursor getData() {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT _id, title, link, description FROM " + TABLE_NEWS, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        cursor.getString(cursor.getColumnIndexOrThrow(cursor.getColumnName(1)));
        cursor.moveToNext();
    }
    return cursor;
}

//Method to delete item from database, on long-pressing listView
public void deleteItemFromTable(String rowId) {
    SQLiteDatabase db = this.getWritableDatabase();

    db.delete(TABLE_NEWS, _id + " = " + rowId, null);

    Log.w("Item has been deleted", "*****************");
    db.close();

}

Aucun commentaire:

Enregistrer un commentaire