How to add ExpandableListView in fragment?

557 views Asked by At

Hello i was following this tutorial for adding ExpandableListView to my application, but i couldn't get to set up that list in fragment. I will put here code for fragment page and for adapter. If there's need for more code i will update. Here's my code for fragment page:

    package com.dusandimitrijevic.fragments;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.dusandimitrijevic.spisakzakupovinu.ExpandableListAdapter;
import com.dusandimitrijevic.spisakzakupovinu.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FragmentProizvodi#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FragmentProizvodi extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;


    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment FragmentProizvodi.
     */
    // TODO: Rename and change types and number of parameters
    public static FragmentProizvodi newInstance(String param1, String param2) {
        FragmentProizvodi fragment = new FragmentProizvodi();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public FragmentProizvodi() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_proizvodi, container, false);   
        ExpandableListAdapter listAdapter;
        ExpandableListView expListView;
        List<String> listDataHeader;
        HashMap<String, List<String>> listDataChild;
        // get the listview
        expListView = (ExpandableListView) view.findViewById(R.id.listView1);

        // preparing list data
        prepareListData();

        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild); //here i'm getting an error now

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

    /*
     * Preparing the list data
     */
    private void prepareListData() {
// and here i'm getting also error 
            listDataHeader = new ArrayList<String>();
            listDataChild = new HashMap<String, List<String>>();

        // Adding child data (Here also)
        listDataHeader.add("Brasno");
        listDataHeader.add("Sokovi");
        listDataHeader.add("Grickalice");

        // Adding child data
        List<String> brasno = new ArrayList<String>();
        brasno.add("Hleba");
        brasno.add("Kore za pitu");
        brasno.add("Heljdino brasno");
        brasno.add("Crno Brasno");
        brasno.add("Zitarice");
        brasno.add("Testo");
        brasno.add("Integralno brasno");

        List<String> sokovi = new ArrayList<String>();
        sokovi.add("Coca Cola");
        sokovi.add("Juice");
        sokovi.add("Saa");
        sokovi.add("DSA");
        sokovi.add("DSS");
        sokovi.add("Tres");

        List<String> grickalice = new ArrayList<String>();
        grickalice.add("Sda");
        grickalice.add("Sadasda");
        grickalice.add("SDada");
        grickalice.add("SDADA");
        grickalice.add("sadad");
 //And every single line down an error
        listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
        listDataChild.put(listDataHeader.get(1), nowShowing);
        listDataChild.put(listDataHeader.get(2), comingSoon);



        return view;
    }
}

I'm new to this and i would appricieate some help. Thank you.

0

There are 0 answers