Android如何将Listview添加到Fragment而不是ListActivity

时间:2014-02-08 11:08:27

标签: android listview fragment

我知道如何将ListView添加到ListActivity,但如何将ListView添加到Fragment

1 个答案:

答案 0 :(得分:0)

让您的课程延伸ListFragment而不是Fragment,如下所示:

public class MyListFragment extends ListFragment {

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
    "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
    "Linux", "OS/2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
    android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}