Android布局设计......在标题上调整

时间:2014-02-24 09:29:50

标签: android android-fragments expandablelistview

我想在我的应用程序中实现tab。标签效果很好。现在在其中一个标签中实现可扩展列表视图...这意味着活动扩展Fragment而不是FragmentActivity ......但我在消耗列表视图中遇到问题...我怎样才能克服这个问题。 ..这是我的代码......

public class Setting extends Fragment{

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

      @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.setting, container, false);


            expListView = (ExpandableListView)rootView. findViewById(R.id.lvExp);

            // preparing list data
            prepareListData();

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

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

        }

    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();

        // Adding child data
        listDataHeader.add("Top 250");
        listDataHeader.add("Now Showing");
        listDataHeader.add("Coming Soon..");

        // Adding child data
        List<String> top250 = new ArrayList<String>();
        top250.add("Remaining Battery");
        top250.add("Set Alarm Distance");
        top250.add("Pick Alarm Tone");
        top250.add("Set Vibrate");

        List<String> nowShowing = new ArrayList<String>();
        nowShowing.add("Remaining Battery");
        nowShowing.add("Set Alarm Distance");
        nowShowing.add("Pick Alarm Tone");
        nowShowing.add("Set Vibrate");


        List<String> comingSoon = new ArrayList<String>();
        comingSoon.add("2 Guns");
        comingSoon.add("The Smurfs 2");
        comingSoon.add("The Spectacular Now");
        comingSoon.add("The Canyons");
        comingSoon.add("Europa Report");

        listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
        listDataChild.put(listDataHeader.get(1), nowShowing);
        listDataChild.put(listDataHeader.get(2), comingSoon);
    }


    }

错误显示

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

1 个答案:

答案 0 :(得分:0)

在构造函数中使用getActivity()代替。我建议您将填充数据移除到方法onActivityCreated()中。确保getActivity()仅在调用此方法后才返回创建片段的活动。这是根据Android开发者文档链接:http://developer.android.com/guide/components/fragments.html

因此,将数据填充移至onActivityCreated()并使用getActivity()作为传递给适配器的上下文