ExpandableListView子数组元素到具有Intent的新页面

时间:2014-04-29 09:19:17

标签: java android expandablelistview

这是我目前正在处理的项目的ExpandableListView布局。我遇到了孩子ArrayList的问题。我希望能够点击每道菜并向我引导一个新页面。我尝试使用意图,但它无法工作。

这是我的Java类代码。

public class Menupage extends ExpandableListActivity {
    private static final String arrGroupElements[] = {
        "Main","Pasta","Pizza", "Salads", "Sides",
        "Desserts", "Soups", "Beverages" };

    /**
     014
     * strings for child elements
     015
     */

    private static final String arrChildElements[][] = {
        { "Fish & Chips","Lamb Chop", "Cajun Fish", "Chicken Chop"},
        { "Spaghetti Bolognese ","Spaghetti Carbonara", "Spaghetti Aglio Olio" },
        { "Hawaiian Pizza","Simply Cheese Pizza", "Classic Pepperoni" },
        { "Chicken Salad","Potatoes Salad", "Greek Salad" },
        { "Garlic Bread" ,"Criss Cross Fries", "Hot & Sweet Wings" },
        { "Banana Boat Ice Cream ","Strawberry Shortcake", "Chocolate Lava" },
        { "Cream of Mushroom Soup","Chicken & Veg Soup", "Tomato Soup" },
        { "Coffee","Tea", "Juices" },
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.article_screen);
        setListAdapter(new ExpandableListAdapter(this));
    }

    public class ExpandableListAdapter extends BaseExpandableListAdapter {
        private Context myContext;

        public ExpandableListAdapter(Context context) {
            myContext = context;
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return null;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return 0;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) myContext
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.child_item_layout, null);

            }

            TextView yourSelection = (TextView) convertView
                    .findViewById(R.id.articleContentTextView);
            yourSelection
                    .setText(arrChildElements[groupPosition][childPosition]);

            return convertView;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return arrChildElements[groupPosition].length;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return null;
        }

        @Override
        public int getGroupCount() {
            return arrGroupElements.length;
        }

        @Override
        public long getGroupId(int groupPosition) {
            return 0;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) myContext
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate( R.layout.article_list_item_layout, null);
            }

            TextView groupName = (TextView) convertView.findViewById(R.id.articleHeaderTextView);
            groupName.setText(arrGroupElements[groupPosition]);

            return convertView;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:1)

在menupage活动中,为可扩展列表视图创建id,然后调用setOnChildClickListener。

  

示例代码和答案:

ExpandableListView expListView = (ExpandableListView) findViewById(R.id.lvExp);
expListView.setOnChildClickListener(new OnChildClickListener() {
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
             Intent nextActivity=new Intent(Menupage.this,NextScreen.class);
             nextActivity.putExtra("SELECTED_CHILD_VALUE", arrChildElements[groupPosition][childPosition]);               
             startActivity(nextActivity);
            return false;
        }
    });