0

I've made an ExpandableListView with ArrayList as following:`

public void setChildGroupData() {

    /**
     * Add Data For TecthNology
     */
    ArrayList<String> child = new ArrayList<String>();
    child.add("Java");
    child.add("Drupal");
    child.add(".Net Framework");
    child.add("PHP");
    childItem.add(child);

    /**
     * Add Data For Mobile
     */
    child = new ArrayList<String>();
    child.add("Android");
    child.add("Window Mobile");
    child.add("iPHone");
    child.add("Blackberry");
    childItem.add(child);
    /**
     * Add Data For Manufacture
     */
    child = new ArrayList<String>();
    child.add("HTC");
    child.add("Apple");
    child.add("Samsung");
    child.add("Nokia");
    childItem.add(child);
    /**
     * Add Data For Extras
     */
    child = new ArrayList<String>();
    child.add("Contact Us");
    child.add("About Us");
    child.add("Location");
    child.add("Root Cause");
    childItem.add(child);


}

Now i want do do an action by triggering an if statement when some of the children is selected:

if(childItem.getText(childPosition) == "Java"){

        mDrawerLayout.closeDrawer(drawerList);

    }

However it is not working. I wonder if there's a way to get text from lets say child.add("Java"); so that if statement work and triggers an action?

2 Answers2

2

Use:

if("Java".equals(childItem.getText(childPosition)))

As == compares references while equals() compares values

Check this for more info

Community
  • 1
  • 1
BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
2

The string comparison string1 == string2 will check the reference not the value of each object

You should use string1.equals(string2), which would compare the values in each object.

In your case it is

if(childItem.getText(childPosition).equals("Java")){

    mDrawerLayout.closeDrawer(drawerList);

}

You can find some cool answers in this post.

Hope it helped solve your problem.

Community
  • 1
  • 1
Adheep
  • 1,585
  • 1
  • 14
  • 27