如何在expandablelistview中单击childitem后打开新活动

问题描述 投票:0回答:2

我正在开发expandablelistview以打开标题然后副标题。但是,当我点击子标题时,它应该打开新的活动。

请在这里帮助我,而不是点击看到孩子项目的吐司。

private OnChildClickListener myListItemClicked =  new OnChildClickListener() {

    public boolean onChildClick(ExpandableListView parent, View v,
                                int groupPosition, int childPosition, long id) {

        //get the group header
        HeaderInfo headerInfo = SectionList.get(groupPosition);
        //get the child info
        DetailInfo detailInfo =  headerInfo.getProductList().get(childPosition);
        //display it or do something with it
        Toast.makeText(getBaseContext(), "Clicked on Detail " + headerInfo.getName()
                + "/" + detailInfo.getName(), Toast.LENGTH_LONG).show();
        return false;
    }
};
android expandablelistview
2个回答
0
投票

将ThisActivity替换为您当前的活动,并将YourNewActivity替换为您想要的新活动

private OnChildClickListener myListItemClicked = new OnChildClickListener() {

    public boolean onChildClick(ExpandableListView parent, View v,
                                int groupPosition, int childPosition, long id) {

        //get the group header
        HeaderInfo headerInfo = SectionList.get(groupPosition);
        //get the child info
        DetailInfo detailInfo =  headerInfo.getProductList().get(childPosition);
        //display it or do something with it
        Toast.makeText(getBaseContext(), "Clicked on Detail " + headerInfo.getName()
                + "/" + detailInfo.getName(), Toast.LENGTH_LONG).show();

    switch(childPosition) {
    case 0:
        Intent intent = new Intent(ThisActivity.this, YourNewActivity0.class);
        break;
    case 1:
        Intent intent = new Intent(ThisActivity.this, YourNewActivity1.class);
        break;
    case 2:
        Intent intent = new Intent(ThisActivity.this, YourNewActivity2.class);
        break;
}

    startActivity(intent);
        return false;
    }
};

0
投票

谢谢@Angus Tay;我从你的回复中得到了想法。我找到了下面给出的答案,它对我有用...

switch(groupPosition) {
                case 0:
                    switch (childPosition) {
                        case 0:
                            Intent c1= new Intent(MainActivity.this,YourNewActivity0.class);
                            startActivity(c1);
                            break;
                        case 1:
                            Intent d2= new Intent(MainActivity.this,YourNewActivity1.class);
                            startActivity(d2);
                            break;
                        case 2:
                            Intent d3= new Intent(MainActivity.this,YourNewActivity2.class);
                            startActivity(d3);
                            break;


                    }
                    break;
                case 1:
                    switch (childPosition) {
                        case 0:
                            Intent d1= new Intent(MainActivity.this,YourNewActivity0.class);
                            startActivity(d1);
                            break;
                        case 1:
                            Intent d2= new Intent(MainActivity.this,YourNewActivity1.class);
                            startActivity(d2);
                            break;


                    }
                    break;
                case 2:
                    switch (childPosition) {
                        case 0:
                            Intent a1= new Intent(MainActivity.this,YourNewActivity0.class);
                            startActivity(a1);
                            break;
                        case 1:
                            Intent a2= new Intent(MainActivity.this,YourNewActivity1.class);
                            startActivity(a2);
                            break;

                    }
            }
            return false;
        }
    };
© www.soinside.com 2019 - 2024. All rights reserved.