0

In my application I'm using a listview. I need to perform an if else check with selected data from listview. For that I created the following code. When I displayed the selected value, it displays correctly. But it isn't checked within the loop.

mainListView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
        String level = (String) (mainListView.getItemAtPosition(myItemInt));
        //  Toast.makeText(Listactivity.this, "" + level, 2000).show(); 

        if(level == "level-1")
        {
            Toast.makeText(Listactivity.this, "" + "selected levelel1", 2000).show();
        }
        else if(level == "level-2")
        {
         Toast.makeText(Listactivity.this, "" + "selected level 2", 2000).show();
        }
        else if(level == "level-3")
        {
         Toast.makeText(Listactivity.this, "" + "selected level3", 2000).show();
        }
        else
        {
         System.out.println("Level s not available");
        }

        }

     });
  }

3 Answers 3

2

Please try compare String do like this

if(level.equalsIgnoreCase("level-1")){
}
Sign up to request clarification or add additional context in comments.

Comments

1

Use .equals().Because == compares Strings Refrences not Characters of strings.

Compares Strings using .equals() when String is object.When you declare string as String literal then you can compare strings using ==

 if(level.equals("level-1"))
        {
            Toast.makeText(Listactivity.this, "" + "selected levelel1", 2000).show();
        }
        else if(level.equals("level-2"))
        {
         Toast.makeText(Listactivity.this, "" + "selected level 2", 2000).show();
        }

2 Comments

now it is working i did a mistake i put 'level-1' instead of 'Level-1'.Thank you so much ..
@PriyaRaj Use level.equalsIgnoreCase("level-1")
1

To compare String do like this

if(level.equals("level-1")){
}

1 Comment

Check your level values.is it equal to "level-1" or "level-2" and so on

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.