0

I want to show "Favorite is empty" when there is no data to retrieve. However, it only work for else statement and it doesn't work in if statement . How can I check it is empty or not. I want to do like this the program will work as I coded when user click the button. But if there is not data to work when user click the button, program should show text from if statement

Here is my code

if(wishlist_item.size()>=1){
                    L1.setOnClickListener(new View.OnClickListener() {
                        String a = wishlist_item.get(0);
                        @Override
                        public void onClick(View view) {
                            if(a.isEmpty()){
                                Toast.makeText(wish_list.this, "Favourite is empty", Toast.LENGTH_SHORT).show();
                            }else{
                                Intent launchWish = new Intent(wish_list.this,web_view2.class);
                                launchWish.putExtra("k",a);
                                startActivity(launchWish);
                            }
                        }
                    });
                }
Indi Indo
  • 15
  • 5
  • "it only work for else statement" what do you mean by that? that it always go straight to else? have you debugged your code? have you add some basic prints/loggings just to check the values of the variables you are comparing? – Stultuske Feb 02 '21 at 07:17
  • else statement work if there is data to show in database. But if statement doesn't work if there is not data to show in database. I want to show the text message like "no book" if user click the button and no data @Stultuske – Indi Indo Feb 02 '21 at 07:20
  • ok, so what is your point? the comparison works perfect, your Toast.makeText call just doesn't do what you think it should do? – Stultuske Feb 02 '21 at 07:21
  • I think if (a.isEmpty) it should work or is there anything that I need to add more in if (a.isEmpty) this code? @Stultuske – Indi Indo Feb 02 '21 at 07:39
  • we don't even know what your code should do. We also don't know what value a has at that point. You are very unclear in what is actually going wrong – Stultuske Feb 02 '21 at 07:51
  • Does this answer your question? [How can I show makeText text if variable is empty](https://stackoverflow.com/questions/66006511/how-can-i-show-maketext-text-if-variable-is-empty) – Toygur Ziya Kutlu Feb 02 '21 at 10:58

2 Answers2

0
if(a.isEmpty()){

most likely a.isEmpty is not working you've got to find another method to ascertain if a is empty or not

HTH traja47

Trajano Roberto
  • 179
  • 2
  • 7
0

Like this?

L1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if(wishlist_item.size() > 0) {
            Intent launchWish = new Intent(wish_list.this,web_view2.class);
            String a = wishlist_item.get(0);
            launchWish.putExtra("k",a);
            startActivity(launchWish);
        } else {
            Toast.makeText(wish_list.this, "Favourite is empty", Toast.LENGTH_SHORT).show();
        }
    }
});
Darkman
  • 2,941
  • 2
  • 9
  • 14