Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I developed a word app that contains English vocabularies and users can add their favorite word by clicking on a floating button. Every time my favorite list has at list one item it is updated but when it's empty it doesn't show anything but when I run app again list updates. if you need any code let me know to send it .

Inner page :

selectDb();

    if(selectFavoriteState()){
        favorite.setImageResource(R.drawable.ic_favorite_like);
    }else {
        favorite.setImageResource(R.drawable.ic_favorite_maylike);
    }

    favorite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (selectFavoriteState()){
                favorite.setImageResource(R.drawable.ic_favorite_maylike);
                updateUnfavorited();

            }else {
                favorite.setImageResource(R.drawable.ic_favorite_like);
                updateFavorited();

            }



        }
    });

 private void selectDb(){
    destpath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ielts/";
    database = SQLiteDatabase.openOrCreateDatabase(destpath + "/md_book.db", null);
}

private boolean selectFavoriteState(){
    @SuppressLint("Recycle") Cursor cursor = database.rawQuery("SELECT * FROM main WHERE id = " + id, null);
    while (cursor.moveToNext()){
        favoriteState = cursor.getString(cursor.getColumnIndex("fav"));

    }
    return favoriteState.equals("1");
}

private void updateFavorited(){
    database.execSQL( "UPDATE main SET fav = 1 WHERE id = " + id);

}

private void updateUnfavorited(){
    database.execSQL( "UPDATE main SET fav = 0 WHERE id = " + id);


}


What I have tried:

I tried many forums but I haven't recieved any good answer.
Posted
Comments
David Crow 19-Feb-18 9:19am    
Where is the code to show the (updated) list? Where do you tell the list to refresh? The code you've shown does not appear to be relevant to the question.
Member 13551964 19-Feb-18 15:04pm    
well this code is for innerpage where that button is clicked and adds favorite or remove it . I have a different class for page fragment. any code you need please let me know to post.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900