簡體   English   中英

ExpandableListView子數組元素到具有Intent的新頁面

[英]ExpandableListView child array elements to a new page with an Intent

這是我當前正在處理的項目的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);
    }
}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM