0

I am developing an application that contains a favorite feature. When the user clicks an image button, it should change its background resource and does something depending on its current background resource. The problem is that I need the button to be continually clickable so that if the user accidentally added an item to favorites, he/she would be able to remove it from favorites.

Here's the onClickListener that does the changes, but it does it only one time inside my activity. Once the user changes the favorite status, he/she can't change it again unless he/she leaves the activity and resumes it again.

ImageButton fav = (ImageButton) findViewById(R.id.fav);    
fav.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (item.getIsFav() == 1) {
                    fav.setImageResource(R.drawable.fav_dimed);
                    editFavorite("remove from favorites");
                } else if (item.getIsFav() == 0) {
                    fav.setImageResource(R.drawable.fav);
                    editFavorite("add to favorites");
                }
            }
        });

Any one to help please :)

  • Probably the variable `isFav` inside the `item` is not updated (always `1` or `0`), hence there is no effect to the subsequent click. Would you please also post the code for `editFavorite()`? – Andrew T. Nov 11 '13 at 03:51
  • I am sure that when the user clicks the button, the value of isFav is changed according to its current value (if 1 it becomes 0, if 0 it becomes 1). The problem is not here, the problem is that the button itself becomes non clickable as soon as the user clicks it once. – AmrHalim Nov 11 '13 at 03:59
  • Use Toggle and Check The Status Of The Toggle If It is On then add in Fav Else Remove – Karan Mavadhiya Nov 11 '13 at 04:28
  • Since the cause is still not clear, I have some questions: How do you store `isFav`? I assume you are using SQLite? If yes, do you update the value directly to the DB, but not to the current `item`? If `editFavorite()` has something to do with updating `isFav`, could you post the relevant code here? And lastly, try adding `Log.i("OnClick", "Clicked");` inside `onClick()`, just above the `if-else` block. See whether the listener always responds to your click or not. – Andrew T. Nov 11 '13 at 04:43
  • Thanks all for your comments. @KaranMavadhiya: The problem is not the type of the used widget, they all do the same. – AmrHalim Nov 11 '13 at 13:34
  • @antimo: I solved it. The problem was, exactly as you said, that I don't change the value of the current item. Thank you very much! :) – AmrHalim Nov 11 '13 at 13:36

1 Answers1

0

It may happen because you might be setting - fav button setClickable - false or setEnabled - false.

Are you sure you don't have something like this in your code ?

fav.setEnabled(false);
fav.setClickable(false);

shaktiman_droid
  • 2,368
  • 1
  • 17
  • 32
  • No I don't. I've just added fav.setClickable(true); and fav.setEnabled(true); inside onClick() method body to make sure that it's clickable, but nothing changed! – AmrHalim Nov 11 '13 at 04:11