无法在ListFragment上使用SimpleAdapter(Cannot use SimpleAdapter on a ListFragment)

编程入门 行业动态 更新时间:2024-10-24 19:14:59
无法在ListFragment上使用SimpleAdapter(Cannot use SimpleAdapter on a ListFragment)

我正在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任何解决方案?

I'm developing a ListView in an Android Fragment. The class extends ListFragment.

I tried with this example: http://www.heikkitoivonen.net/blog/2009/02/15/multicolumn-listview-in-android/

but the problem is that constructor SimpleAdapter is not defined if the class extends ListFragment, changing it to ListActivity would make SimpleAdapter work, but then application won't.

Here's the code:

@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; }

So I will have no problem if this was an Activity, but it's a Fragment :S Any solution?

最满意答案

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

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

SimpleAdapter mSchedule = new SimpleAdapter(getActivity(), mylist, ...

Finally found solution by using a custom adapter: http://codehenge.net/blog/2011/05/customizing-android-listview-item-layout/

更多推荐

本文发布于:2023-07-19 20:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1186723.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ListFragment   SimpleAdapter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!