2012-07-11 37 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