4

我正在 Android 片段中开发 ListView。该类扩展了 ListFragment。

我试过这个例子: http ://www.heikkitoivonen.net/blog/2009/02/15/multicolumn-listview-in-android/

但问题是,如果类扩展 ListFragment,则未定义构造函数 SimpleAdapter,将其更改为 ListActivity 将使 SimpleAdapter 工作,但应用程序不会。

这是代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View tmp_view = inflater.inflate(R.layout.clients_list, container, false);
    ListView list = (ListView) tmp_view.findViewById(R.id.list);

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("train", "101");
    map.put("from", "6:30 AM");
    map.put("to", "7:40 AM");
    mylist.add(map);
    map = new HashMap<String, String>();
    map.put("train", "103(x)");
    map.put("from", "6:35 AM");
    map.put("to", "7:45 AM");
    mylist.add(map);
    // ...
    SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.clients_list_item,
                new String[] {"train", "from", "to"}, new int[] {R.id.TRAIN_CELL, R.id.FROM_CELL, R.id.TO_CELL});
    list.setAdapter(mSchedule);
    ListFragment.setListAdapter(mSchedule);


    return tmp_view;
}

因此,如果这是一个活动,我将没有问题,但它是一个片段:S 任何解决方案?

4

2 回答 2

5

ListFragment 不是构造函数需要的 Context 的子类。尝试更换

SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, ...

SimpleAdapter mSchedule = new SimpleAdapter(getActivity(), mylist, ...
于 2013-08-13T00:58:54.770 回答
3

最后通过使用自定义适配器找到了解决方案:http: //codehenge.net/blog/2011/05/customizing-android-listview-item-layout/

于 2011-11-03T09:57:49.943 回答