在 android 的片段中设置列表视图适配器

发布于 2024-12-07 12:34:42 字数 1314 浏览 1 评论 0原文

我想要一个自定义行,因此我在 xml 中使用列表视图并膨胀为片段。我对如何设置列表视图的适配器感到非常困惑。 我创建了一个扩展基本适配器的新适配器。在 getView 方法中,我真的不知道在膨胀 row.xml 布局时要传递什么上下文。 如何设置列表视图的适配器以及在哪里设置?

public class ResultsFragment extends Fragment{


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.results_layout, container, false);
        listView = (ListView)v.findViewById(R.id.results);
        return v;
    }

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


        loadPage(dataBean.getWhat(), dataBean.getWhere(), dataBean.getPageStart());

        //resultsAdapter.setRssData(rssData);
        //setListAdapter(resultsAdapter);
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        Context context = getActivity().getApplicationContext();
        resultsAdapter = new ResultsAdapter(context);
    }





    /**
     * Set List Adapter
     */
    private void setAdapter(){
        if(listView.getAdapter() == null){
            listView.setAdapter(resultsAdapter);
        }
        else{
            resultsAdapter.notifyDataSetChanged();
        }
    }


}

I want a custom row, so I am using a List View in an xml and inflating into a fragment. I am very confused of how to set the adapter for the list View.
I created a new adapter which extends Base Adapter. In the getView method, I really don't know what context to pass while inflating the row.xml layout.
How do I set the adapter for the list view and where?

public class ResultsFragment extends Fragment{


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.results_layout, container, false);
        listView = (ListView)v.findViewById(R.id.results);
        return v;
    }

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


        loadPage(dataBean.getWhat(), dataBean.getWhere(), dataBean.getPageStart());

        //resultsAdapter.setRssData(rssData);
        //setListAdapter(resultsAdapter);
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        Context context = getActivity().getApplicationContext();
        resultsAdapter = new ResultsAdapter(context);
    }





    /**
     * Set List Adapter
     */
    private void setAdapter(){
        if(listView.getAdapter() == null){
            listView.setAdapter(resultsAdapter);
        }
        else{
            resultsAdapter.notifyDataSetChanged();
        }
    }


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

五里雾 2024-12-14 12:34:42

您必须扩展 Listfragment(而不是 Fragment),并使用其 ListFragment.setListAdapter 来设置适配器。在适配器 getView() 中膨胀您的行..仅此而已

You must extend Listfragment (instead of Fragment), and using its ListFragment.setListAdapter to set your adapter. In the adapter getView() inflate your row.. that s all

初见 2024-12-14 12:34:42

如果您不想更改扩展类,则应该使用 listview.setAdapter(...) 方法。正如您在我的示例中看到的:

ListView productList= (ListView) getActivity().findViewById(R.id.product_list);


    SampleAdapter adapter = new SampleAdapter(getActivity());


    adapter.add(new SampleItem(
            "Sunny LCD TV 2\"  SN022L66-T1 Full HD",
            R.drawable.product_sample_pic);


    productList.setAdapter(adapter);

If you do not want to change your extended class, you should use listview.setAdapter(...) method. As you see in my example :

ListView productList= (ListView) getActivity().findViewById(R.id.product_list);


    SampleAdapter adapter = new SampleAdapter(getActivity());


    adapter.add(new SampleItem(
            "Sunny LCD TV 2\"  SN022L66-T1 Full HD",
            R.drawable.product_sample_pic);


    productList.setAdapter(adapter);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文