-3

I'm pretty new to Fragment based activities. I'm working on a collapsible listview inside the CollapsingToolbarLayout.

Here, I'm facing Unreachable statement error in java file. Not able to get rid of it.

My code -

public class MenuActivity extends android.support.v4.app.Fragment {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_booktable, container, false);

        // get the listview
        expListView = (ExpandableListView) container.findViewById(R.id.lvExp);

        // preparing list data
        prepareListData();

        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);

        // Listview Group click listener
        expListView.setOnGroupClickListener(new OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                                        int groupPosition, long id) {
                // Toast.makeText(getApplicationContext(),
                // "Group Clicked " + listDataHeader.get(groupPosition),
                // Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        // Listview Group expanded listener
        expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                Toast.makeText(getActivity().getApplicationContext(),
                        listDataHeader.get(groupPosition) + " Expanded",
                        Toast.LENGTH_SHORT).show();
            }
        });

        // Listview Group collasped listener
        expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {
                Toast.makeText(getActivity().getApplicationContext(),
                        listDataHeader.get(groupPosition) + " Collapsed",
                        Toast.LENGTH_SHORT).show();

            }
        });

        // Listview on child click listener
        expListView.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                // TODO Auto-generated method stub
                Toast.makeText(
                        getActivity().getApplicationContext(),
                        listDataHeader.get(groupPosition)
                                + " : "
                                + listDataChild.get(
                                listDataHeader.get(groupPosition)).get(
                                childPosition), Toast.LENGTH_SHORT)
                        .show();
                return false;
            }
        });
    }

    /*
     * Preparing the list data
     */
    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();

        // Adding child data
        listDataHeader.add("South Indian");
        listDataHeader.add("Quick Bites");
        listDataHeader.add("Soups");

        // Adding child data
        List<String> SouthIndian = new ArrayList<String>();
        SouthIndian.add("Kara Bath");
        SouthIndian.add("Chow Chow Bath");
        SouthIndian.add("2 Idli 1 Vada");
        SouthIndian.add("Rava Idli");
        SouthIndian.add("Curd Vada");
        SouthIndian.add("Masala Dosa");

        List<String> QuickBites = new ArrayList<String>();
        QuickBites.add("Veg Sandwich");
        QuickBites.add("Veg Toast Sandwich");
        QuickBites.add("Bread Butter Jam");
        QuickBites.add("Bread Jam");
        QuickBites.add("Pakoda");
        QuickBites.add("Masala Puri");

        List<String> Soups = new ArrayList<String>();
        Soups.add("Tomato Soup");
        Soups.add("Sweet corn Soup");
        Soups.add("Veg Soup");
        Soups.add("Veg Schezwan Soup");
        Soups.add("Veg Noodles Soup");

        listDataChild.put(listDataHeader.get(0), SouthIndian); // Header, Child data
        listDataChild.put(listDataHeader.get(1), QuickBites);
        listDataChild.put(listDataHeader.get(2), Soups);
    }
}

How do I fix the error for the line expListView = (ExpandableListView) container.findViewById(R.id.lvExp); and make this code run?

Abhishek D
  • 465
  • 2
  • 9
  • 24

2 Answers2

1

move return inflater.inflate(R.layout.activity_booktable, container, false) to the end of method onCreateView()

Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
0

Your first line in onCreateView already returns the View so the rest of the code is not executed:

return inflater.inflate(R.layout.activity_booktable, container, false);

You have to replace it with

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        
    View myView = inflater.inflate(R.layout.activity_booktable, container, false);
    ...
    return myView;
}

and then better do the findViewById on this view, e.g.

expListView = (ExpandableListView) myView.findViewById(R.id.lvExp);
PhilLab
  • 4,777
  • 1
  • 25
  • 77