【问题标题】:Expandable ListView in Fragment片段中的可扩展 ListView
【发布时间】:2013-07-14 05:16:52
【问题描述】:

我有一个可工作的可扩展列表视图代码,它作为一个独立的工作正常。 我有另一个用于滑动标签视图的工作代码,我单独编写。

这两篇文章都是在阅读了许多博客、android dev 和 stackoverflow 问题后编写的。

当我尝试将可扩展列表视图合并并放入其中一个片段时,我最终遇到了一个错误,我无法解决。

这是片段中的代码:

public class Fragment1 extends Fragment {
private static final String NAME = "NAME";
private static final String IS_EVEN = "IS_EVEN";

private ExpandableListAdapter mAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
    List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
    for (int i = 0; i < 10; i++) {
        Map<String, String> curGroupMap = new HashMap<String, String>();
        groupData.add(curGroupMap);
        curGroupMap.put(NAME, "Group " + i);
        curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");

        List<Map<String, String>> children = new ArrayList<Map<String, String>>();
        for (int j = 0; j < 2; j++) {
            Map<String, String> curChildMap = new HashMap<String, String>();
            children.add(curChildMap);
            curChildMap.put(NAME, "Child " + j);
            curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
        }
        childData.add(children);
    }

    // Set up our adapter
    mAdapter = new SimpleExpandableListAdapter(
            getActivity(),
            groupData,
            android.R.layout.simple_expandable_list_item_1,
            new String[] { NAME, IS_EVEN },
            new int[] { android.R.id.text1, android.R.id.text2 },
            childData,
            android.R.layout.simple_expandable_list_item_2,
            new String[] { NAME, IS_EVEN },
            new int[] { android.R.id.text1, android.R.id.text2 }
            );
    setListAdapter(mAdapter);
}

}`

最后一行总是报错:'The method setListAdapter(ExpandableListAdapter) is undefined for the type Fragment1'。

其余代码没有显示错误。

请帮忙。我该如何解决这个问题。

【问题讨论】:

  • 可能对你有帮助 gist.github.com/mosabua/1316903
  • 谢谢.. 我试过这个例子,但是当我在 AVD 上运行它时没有得到任何列表视图,它在片段中给出了一个 blenk 屏幕。

标签: android android-fragments expandablelistview


【解决方案1】:

我自己解决了这个问题。为了将来参考,这里是代码。可能很乱,需要清理,但现在可以了!

  public class HotDeals extends Fragment {
private ExpandableListAdapter mAdapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,                   Bundle              savedInstanceState) {
    View v = inflater.inflate(R.layout.saved_tabs, null);

return v;
}

private static final String NAME = "NAME";
private static final String IS_EVEN = "IS_EVEN";

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
    List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
    for (int i = 0; i < 10; i++) {
        Map<String, String> curGroupMap = new HashMap<String, String>();
        groupData.add(curGroupMap);
        curGroupMap.put(NAME, "Group " + i);
        curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");

        List<Map<String, String>> children = new ArrayList<Map<String, String>>();
        for (int j = 0; j < 2; j++) {
            Map<String, String> curChildMap = new HashMap<String, String>();
            children.add(curChildMap);
            curChildMap.put(NAME, "Child " + j);
            curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This          child     is odd");
        }
        childData.add(children);
    }
    ExpandableListView lv = (ExpandableListView) getActivity().findViewById(R.id.list);
    // Set up our adapter
    mAdapter = new SimpleExpandableListAdapter(
            getActivity(),
            groupData,
            android.R.layout.simple_expandable_list_item_1,
            new String[] { NAME, IS_EVEN },
            new int[] { android.R.id.text1, android.R.id.text2 },
            childData,
            android.R.layout.simple_expandable_list_item_2,
            new String[] { NAME, IS_EVEN },
            new int[] { android.R.id.text1, android.R.id.text2 }
            );
    lv.setAdapter(mAdapter);
}
}

@Divers 和 @Dixit - 感谢。

【讨论】:

    【解决方案2】:

    Fragment 不包含方法setListAdapterListFragment 做。你必须从这个类中扩展你的片段来解决这个问题。

    【讨论】:

    • 试过 ListFragment,我得到的错误是:'ListFragment 类型中的方法 setListAdapter(ListAdapter) 不适用于参数 (ExpandableListAdapter)'。
    【解决方案3】:

    这篇文章已经有将近 2 年的历史了,但此页面中没有任何答案是相关的。

    回答采购订单问题,

    方法 setListAdapter(ExpandableListAdapter) 未为类型 Fragment1 定义

    这是因为 setListAdapter 是 ListFragment 或 ListActivity 的方法,它正在等待 ListAdapter 或其子类作为参数。你得到这个错误是因为你给了它一个ExpandableListAdapter,它没有实现ListAdapter接口。

    希望这会对某人有所帮助。

    【讨论】:

    • 是的,但这并不能回答“我该如何解决这个问题?”的问题
    猜你喜欢
    • 2019-05-21
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多