0

我想在我的應用程序的兩個選項卡之一中用listview充滿一個片段。片段不加載

但是,什麼我得到這一切(這推移和永遠..)

enter image description here

這就是我想要做的事。

ListFragment.Java

public class ListFragment extends Fragment { 

    Other methods 
    ... 
    //some code 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 


     java.util.List<HashMap<String, String>> aList = new ArrayList<HashMap<String,String>>(); 

     for(int i=0;i<len;i++){ 
      HashMap<String, String> hm = new HashMap<String,String>(); 
      hm.put("tit", "Title : " + title[i]); 
      hm.put("init","Initial Price : " + cur_price[i]); 
      hm.put("cur","Current Price : " + init_price[i]); 
      hm.put("img", d[i].toString()); 
      aList.add(hm); 
     } 



     // Keys used in Hashmap 
     String[] from = { "tit","init","cur","img"}; 

     // Ids of views in listview_layout 
     int[] to = { R.id.tit,R.id.init,R.id.cur,R.id.img}; 

     // Instantiating an adapter to store each items 
     // R.layout.listview_layout defines the layout of each item 
     SimpleAdapter adapter = new SimpleAdapter(context, aList, R.layout.listview_layout, from, to); 

     // Getting a reference to listview of fragment_list_items.xml layout file 
     ListView listView = (ListView) view.findViewById(R.id.listview); 

     // Setting the adapter to the listView 
     listView.setAdapter(adapter); 

     return inflater.inflate(R.layout.fragment_list_items, container, false); 

    } 

問題出在哪裏嗎?

編輯:當我沒有標籤,但只有一個單獨的活動顯示相同的代碼工作正常。

我的佈局文件:

fragment_list_items.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" tools:context="com.example.nikhil.amazon1.ListFragment"> 

    <ListView 
     android:id="@+id/listview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     /> 

</FrameLayout> 

ListView.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="horizontal" 
    > 

    <ImageView 
     android:id="@+id/img" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:contentDescription="" 
     android:paddingTop="10dp" 
     android:paddingRight="10dp" 
     android:paddingBottom="10dp" 
     /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     > 
     <TextView 
      android:id="@+id/tit" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="15sp" 
      /> 

     <TextView 
      android:id="@+id/init" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="15sp" 
      /> 

     <TextView 
      android:id="@+id/cur" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="15sp" 
      /> 
    </LinearLayout> 
</LinearLayout> 

回答

0

首先,你必須通過吹氣創建視圖的,因爲你無法從它findItemById。 並重新命名你的班級。 ListFragment - > otherName。

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 

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

    java.util.List<HashMap<String, String>> aList = new ArrayList<HashMap<String,String>>(); 

    for(int i=0;i<len;i++){ 
     HashMap<String, String> hm = new HashMap<String,String>(); 
     hm.put("tit", "Title : " + title[i]); 
     hm.put("init","Initial Price : " + cur_price[i]); 
     hm.put("cur","Current Price : " + init_price[i]); 
     hm.put("img", d[i].toString()); 
     aList.add(hm); 
    } 



    // Keys used in Hashmap 
    String[] from = { "tit","init","cur","img"}; 

    // Ids of views in listview_layout 
    int[] to = { R.id.tit,R.id.init,R.id.cur,R.id.img}; 

    // Instantiating an adapter to store each items 
    // R.layout.listview_layout defines the layout of each item 
    SimpleAdapter adapter = new SimpleAdapter(context, aList, R.layout.listview_layout, from, to); 

    // Getting a reference to listview of main.xml layout file 
    ListView listView = (ListView) view.findViewById(R.id.listview); 

    // Setting the adapter to the listView 
    listView.setAdapter(adapter); 

    return view; 

} 
+0

對不起,我試過了,但結果相同! :) – Nikhil 2015-03-13 18:03:10

+0

你在你的片段中只有列表視圖嗎? – 2015-03-13 18:34:35

+0

是的,我只有listview。我將用我的XML佈局文件更新這個問題。任何幫助pl? :) – Nikhil 2015-03-14 17:07:43