您好我正在按照本教程将ExpandableListView添加到我的应用程序中,但我无法在片段中设置该列表。我会在这里放置片段页面和适配器的代码。如果需要更多代码,我会更新。这是片段页面的代码:
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;
}
}
我是新手,我会给予一些帮助。谢谢。