使用带有2D数组的If语句

时间:2013-05-23 11:19:48

标签: android android-intent if-statement expandablelistview

我正在与ExpandableListView合作。如果条件是在2D数组中,我需要帮助如何使用if语句。我已经编写了这段代码,但是当我点击每个孩子时,它总是会进入游戏活动。我想要的是每个孩子被挖掘,新的活动(取决于孩子)将被打开。

我是OOP的新手。也许你可以帮助我。谢谢!

listView.setOnChildClickListener(new OnChildClickListener() {
    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        if ( CHILDREN[0][0] == "Management") {
            Intent intent = new Intent(Information.this, Games.class);
            startActivity(intent);
        }
        else if ( CHILDREN[0][1] == "Accountant") {
            Intent intent = new Intent(Information.this, Test.class);
            startActivity(intent);
        }
        else if ( CHILDREN[0][2] == "Economy") {
            Intent intent = new Intent(Information.this, Chat.class);
            startActivity(intent);
        }
        else {
            Intent intent = new Intent(Information.this, MainActivity.class);
            startActivity(intent);
        }
        return true;
    }
});

如果有帮助 - 这是字符串数组声明:

     private String[][] CHILDREN = { 
        { "Management", "Accountant", "Economy" },
        { "IAB", "Communication" , "Hospitality" },
        { "English", "Theology", "BK", "Elementary" },
        { "Machine", "Electrical", "Industrial" },
        { "Law" },
        { "Doctor" },
        { "Psychology" },
        { "Biology" },
};

3 个答案:

答案 0 :(得分:0)

尝试使用CHILDREN[0][0].equals("Management"))代替CHILDREN[0][0] == "Management"

答案 1 :(得分:0)

您应该使用ref childPosition来构建if语句 像这样

String selString = CHILDREN[groupPosition][childPosition];//parent.getItemAtPosition(childPosition).toString();

然后

 if ( selString.equals("Management"))
            {
                Intent intent = new Intent(Information.this, Games.class);
                startActivity(intent);
            }

依旧......

答案 2 :(得分:0)

listView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
    if ( CHILDREN[groupPosition][childPosition] == "Management") {
        Intent intent = new Intent(Information.this, Games.class);
        startActivity(intent);
    }
    else if ( CHILDREN[groupPosition][childPosition] == "Accountant") {
        Intent intent = new Intent(Information.this, Test.class);
        startActivity(intent);
    }
    else if ( CHILDREN[groupPosition][childPosition] == "Economy") {
        Intent intent = new Intent(Information.this, Chat.class);
        startActivity(intent);
    }
    else {
        Intent intent = new Intent(Information.this, MainActivity.class);
        startActivity(intent);
    }
    return true;
}
});

试试这个(希望,最终代码:))