0

我有一个片段 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;
}
4

5 回答 5

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);

如果您想稍后进行修改,可以将列表视图存储在字段中。

于 2013-09-03T08:15:02.227 回答
1
  1. 添加ListView到布局tab1.xml并为此列表提供 id。例如:my_list
  2. 更新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;
    }
    
于 2013-09-03T08:30:52.560 回答
0

Use like

public class Tab1Fragment extends ListFragment {

}
于 2013-09-03T08:10:23.030 回答
0

您可以将活动扩展到ListFragment,这将与ListActivity.

或者你必须像这样设置你的列表适配器:

ListView mListView = (ListView)findViewById(R.Id.your_list_view);

设置列表适配器

mListView.setAdapter(YOUR_LIST_ADAPTER);
于 2013-09-03T08:20:46.733 回答
0

您可以通过扩展 BaseAdapter 来创建自定义列表视图类,而无需更改 android 中的任何扩展类。这是示例代码:

public class Simple_Fragment extends Fragment 
{


   class MyAdapter extends BaseAdapter {
  // here you adpter methods and impelement what do you want

        }

}

于 2013-09-03T08:16:58.747 回答