English 中文(简体)
set fragment
原标题:set list view adapter in a fragment in android

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


}
最佳回答

您必须延长<代码>Listfragment(而不是Fragment),并使用其<代码>ListFragment.setListAdapter,以建立你的适应者。 在改编者中,getView(>> in缩了你的行文。

问题回答

如果你不想改变你的扩大类别,你应使用<条码>清单概览.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);




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签