2012-07-11 53 views
1

我已经看到关于如何将活动转换为ListActivity来填充ListView的各种教程,但没有ListFragments。我有两个选项卡,每个选项卡下都有一个片段。这是我的片段目前的样子....可以有人给我一些想法,我怎么能做到这一点?如何在Android中填充ListFragment?

public class MainFragment extends ListFragment {  

String[] items = { "12 Monkeys", "(500) Days of Summer", "Chariots of Fire" }; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){  
     return inflater.inflate(R.layout.fragment_main, container, false); 
} 
} 

这里是XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 

</LinearLayout> 

我试图让items在ListView显示。

回答

3

您不需要在ListFragment的内部对包含listviewview充气。简单地创建一个adapter,给它一些数据并在onCreate()fragment调用setListAdapter()。然后在其他地方(大概在你的Activity),你可以添加这个fragmentfragmentmanager