我有一个片段Tab1Fragment,它扩展了Fragment类(所以我不能扩展ListActivity,这是调用setListAdapter方法所必需的)。在这个片段中,我想显示一个填充了自定义对象的列表视图。
有没有办法,我可以在不将扩展类更改为ListFragment的情况下设置适配器?问题出在setListAdapter方法(用#HERE标记)。
这是片段代码:
public class Tab1Fragment extends Fragment {
private ArrayList<MobileNETDistinctChatInfo> m_parts = new ArrayList<MobileNETDistinctChatInfo>();
private Runnable viewParts;
private MobileNETDistinctChatInfoAdapter m_adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
MobileNETDBHandler db = new MobileNETDBHandler(getActivity());
// instantiate Adapter class
m_adapter = new MobileNETDistinctChatInfoAdapter(getActivity(), R.layout.chatlist_list_item, m_parts);
setListAdapter(m_adapter); # HERE
// Thread, which fetches data in the background
viewParts = new Runnable(){
public void run(){
handler.sendEmptyMessage(0);
}
};
Thread thread = new Thread(null, viewParts, "MagentoBackground");
thread.start();
return (LinearLayout) inflater.inflate(R.layout.tab1, container, false);
}
private Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
m_parts.add(new MobileNETDistinctChatInfo("someone@gmail.com","Message1", "2013-01-01 11:11:11"));
m_parts.add(new MobileNETDistinctChatInfo("someonelse@gmail.com","Message2", "2013-01-01 11:11:11"));
m_parts.add(new MobileNETDistinctChatInfo("someonelselse@gmail.com","Message3", "2013-01-01 11:11:11"));
m_adapter = new MobileNETDistinctChatInfoAdapter(getActivity(), R.layout.chatlist_list_item, m_parts);
// display the list.
setListAdapter(m_adapter); # HERE
}
};
}
我不能使用ListFragment,因为另一个类中的一个方法,它返回标签(它们被命名为Tab1Fragment,Ta2Fragment,Tab3Fragment并扩展Fragment类),这是Fragment类型,所以我不应该扩展ListFragment。
方法:
@Override
public Fragment getItem(int position) {
Fragment fragment = new Fragment();
switch (position) {
case 0:
return fragment = new Tab1Fragment();
case 1:
return fragment = new Tab2Fragment();
case 2:
return fragment = new Tab3Fragment();
default:
break;
}
return fragment;
}
答案 0 :(得分:2)
您需要获取要填充的ListView对象的引用 看看你的代码,我相信它是在你正在膨胀的LinearLayout中定义的,所以就这样做
LinearLayout l = (LinearLayout) inflater.inflate(R.layout.tab1, container, false);
ListView list = (ListView) l.findViewById(R.id.my_listview_id);
list.setAdapter(m_adapter);
如果您想稍后进行修改,可以将列表视图存储在字段中。
答案 1 :(得分:1)
ListView
添加到布局tab1.xml
并为此列表指定ID。例如:my_list
将onCreateView()
中的Tab1Fragment
方法更新为:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LinearLayout view = (LinearLayout) inflater.inflate(R.layout.tab1, container, false);
ListView list = (ListView)view.findViewById(R.id.my_list);
MobileNETDBHandler db = new MobileNETDBHandler(getActivity());
// instantiate Adapter class
m_adapter = new MobileNETDistinctChatInfoAdapter(getActivity(), R.layout.chatlist_list_item, m_parts);
list.setListAdapter(m_adapter); # HERE
// Thread, which fetches data in the background
viewParts = new Runnable(){
public void run(){
handler.sendEmptyMessage(0);
}
};
Thread thread = new Thread(null, viewParts, "MagentoBackground");
thread.start();
return view;
}
答案 2 :(得分:0)
使用
public class Tab1Fragment extends ListFragment {
}
答案 3 :(得分:0)
您可以通过扩展BaseAdapter来创建自定义列表视图类,而无需更改android中的任何扩展类。 以下是此示例代码:
public class Simple_Fragment extends Fragment
{
class MyAdapter extends BaseAdapter {
// here you adpter methods and impelement what do you want
}
}
答案 4 :(得分:0)
您可以将活动扩展到ListFragment
,这与ListActivity
的工作方式相同。
或者您必须像下面这样设置列表适配器:
ListView mListView = (ListView)findViewById(R.Id.your_list_view);
设置列表适配器
mListView.setAdapter(YOUR_LIST_ADAPTER);