2016-11-30 39 views
1

我試圖在片段中顯示listView,但是當我運行應用程序時,它會給出一個空白頁面而不是。我使用陣列適配器從片段中的字符串填充ListView。無法看到我的片段中的ListView

這裏是我的代碼:

public class CardContentFragment extends Fragment { 


private String[] items; 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 

    super.onActivityCreated(savedInstanceState); 

} 

@Override 
public View onCreateView(LayoutInflater inflater, 
         ViewGroup container, 

         Bundle savedInstanceState) { 

    View view = inflater 
      .inflate(R.layout.item_card, container, false); 


    items = getActivity().getResources().getStringArray(R.array.list); 


    ListView lv = (ListView) view.findViewById(R.id.list); 


    ListAdapter myListAdapter = (new ArrayAdapter<String>(getActivity().getApplicationContext(), 
      android.R.layout.simple_list_item_1, list)); 

    lv.setAdapter(myListAdapter); 


    return view; 


} 


} 

array.xml

<string-array name="list"> 
     <item>Argentina (54 11) 4863 3291</item> 
     <item>Aruba (297) 839 662</item> 
     <item>Australia (61 2) 9416 2373</item> 



    </string-array> 

layout.xml

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ListView xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/list" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:fillViewport="true"> 


    </ListView> 



</RelativeLayout> 

有沒有錯誤,我認爲這只是一個佈局問題,但我無法解決。

+0

顯示一個代碼,你如何在裏面附加fragment活動 – Rahul

回答

1

發現的問題,這只是一個錯字:

ListAdapter myListAdapter = (new ArrayAdapter<String>(getActivity().getApplicationContext(), 
      android.R.layout.simple_list_item_1, items)); 

我只是把「項目」,而不是「名單」

4

你需要通過你的items到您的適配器

ListAdapter myListAdapter = (new ArrayAdapter<String>(getActivity().getApplicationContext(), 
     android.R.layout.simple_list_item_1, items)); 
              ^^^^^ this has your array 
0

試試這個

items = getActivity().getResources().getStringArray(R.array.list); 


    ListView lv = (ListView) view.findViewById(R.id.list); 


    ListAdapter myListAdapter = (new ArrayAdapter<String>(getActivity().getApplicationContext(), 
      android.R.layout.simple_list_item_1, items)); 

    lv.setAdapter(myListAdapter); 

並把你放在你的string.xml中

<string-array name="list"> 
    <item>Argentina (54 11) 4863 3291</item> 
    <item>Aruba (297) 839 662</item> 
    <item>Australia (61 2) 9416 2373</item> 



</string-array>