我的Fragment中的一行代码似乎存在一个问题,它包含Expandable ListView。这件事是,我按照本教程介绍了如何在活动中创建一个http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/。我在活动中的不同工作区中正确地工作了这个。
现在我正在尝试在片段中实现相同的代码。我被遗忘了好几天。下面是Expandable ListView的第一个代码。
SuikodenFragment.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
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.CheckBox;
import android.widget.ExpandableListView;
public class SuikodenFragment extends Fragment {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.suikoden_main_activity1, container, false);
// you can use findViewById() using the above 'view'
// get the listview
expListView = (ExpandableListView) rootView.findViewById(R.id.suikodenList1);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
return rootView;
}
错误是 - 构造函数ExpandableListAdapter(SuikodenFragment,List,HashMap&gt;)未定义
在这一行 -
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
答案 0 :(得分:0)
您必须将上下文传递给ExpandableListAdapter。您正尝试使用'this'传递片段:
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
而是使用以下内容:
listAdapter = new ExpandableListAdapter(getActivity().getApplicationContext(), listDataHeader, listDataChild);
或
listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);