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

在android中的片段中设置列表视图适配器,android,listview,adapter,fragment,Android,Listview,Adapter,Fragment,我想要一个自定义行,所以我在xml中使用列表视图并将其膨胀成一个片段。我对如何为列表视图设置适配器感到非常困惑。 我创建了一个扩展基本适配器的新适配器。在getView方法中,我真的不知道膨胀row.xml布局时要传递什么上下文。 如何设置列表视图的适配器以及在何处设置 public class ResultsFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflate

我想要一个自定义行,所以我在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();
        }
    }


}

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

如果不想更改扩展类,则应使用
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);

结果适配器(getActivity());我想这应该可以。无法从类型对非静态方法setListAdapter(ListAdapter)进行静态引用ListFragment@SureshParmar,它不是一种静态方法。如果您正在扩展
ListFragment
,则该方法位于
@Johnson页面未找到:(它已解决,但请帮助我解决此问题。)