samedi 30 mai 2015

Let user add/remove favorites from drawer/listview from position and save it to SQLite db

I have a sqlite database prepopulated with some data and I would like to have method/function that enables the user to "save" the info in his favorites and display it. The only problem is i have problems with the queries to work properly. I don't understand how to get the position from the current displayed data (from the db) in the activity and save it to the database. I have a column called "Favorites" in db with the default value set to "no" and when the user click a button the value should change to "yes".

The user click a row in listview populated from the DB and it starts a new activity with intent.putExtra(data from db). The new activity displays the data in a single textview. In this new activity i've made a navigation drawer/listview(shows by sliding or "hamburger meny"). In this nav drawer I have a "button" that i would like to get the id from the db according to the current displayed data and change the value of the column "Favorite". In a another class i used

Cursor c = dbHelper.showBook(Integer.toString(position + 1));

Got this help from another recent thread of mine and it worked fine but my new problem is that i have if statements as below. The start of intent works fine. But I want to call a method from my dbHelper to update rows in db. And the code above dont work(but no errors) when i try to use it. The toast shows but nothing is happening in the DB.

myDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){

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

if (position == 0){
                Intent iHome = new Intent(DisplayBook.this, MyAccont.class);
                startActivity(iHome);
 else if (position == 3){
                dbHelper.addFavorites(Integer.toString(position+ 1));
                Toast.makeText(getApplicationContext(), "Added to favorites",    Toast.LENGTH_SHORT).show();

the dbHelper.addFavorites: (the addFavs param is supposed to be the id/pos of the db row as in the working code above) public void addFavorites(String addFavs){

SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_FAV, "yes");
String whereClause = COL_ID + " = ?" ;
String[] whereArgs = new  String[] { addFavs};

          db.update(
          TABLE_NAME,
          values,
          whereClause,
          whereArgs);

}

I've tried a fair amount of versions och different codes and stuff but nothing works and now I'm asking for som advice on this. How can i do it in a good an efficient way? Thank you so much for any help at all!

Aucun commentaire:

Enregistrer un commentaire