【问题标题】:Android layout design..adjust on headingAndroid布局设计..调整标题
【发布时间】:2014-02-24 09:29:49
【问题描述】:

我想在我的应用程序中实现选项卡。标签效果很好。现在在其中一个选项卡中实现可扩展列表视图...这意味着活动扩展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);

【问题讨论】:

  • 错误是什么?发布 logcat。
  • 构造函数 ExpandableListView(Setting, List, HashMap>) 未定义
  • 尝试使用getActivity(); 代替this 关键字。或者getActivity().getAplicationContext();

标签: android android-fragments expandablelistview


【解决方案1】:

在构造函数中使用 getActivity() 而不是 this。我建议您将填充数据删除到方法onActivityCreated()。因为getActivity() 确保只有在调用此方法后才能返回创建片段的活动。这是根据 Android Developers 文档链接:http://developer.android.com/guide/components/fragments.html

因此,删除您在onActivityCreated() 中的数据填充,并使用getActivity() 作为传递给适配器的上下文

【讨论】:

    猜你喜欢
    • 2015-10-13
    • 2016-07-17
    • 2012-08-21
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多