0

我现在正在和他一起工作ExpandableListView。如果条件在二维数组中,我需要帮助如何使用 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" },
};
4

3 回答 3

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;
}
});

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

于 2013-05-23T12:02:40.387 回答
0

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

于 2013-05-23T11:23:15.403 回答
0

你应该用 ref 建立你的 if 语句来childPosition 喜欢这个

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

然后

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

等等...

于 2013-05-23T11:24:55.617 回答