2017-07-24 119 views
1

我創建了一個ListView與片段內的類的自定義對象,並且ListView在模擬器中是空白的,所以我不完全確定我忘記了什麼。這裏有onCreateView()方法,適配器,對象,對象類和佈局文件。ListView不顯示片段中的數據

onCreateView

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    View root = inflater.inflate(R.layout.fragment_kitchen, container, false); 
    add = (Button) root.findViewById(R.id.add); 
    add.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      spinnerDialog.showSpinerDialog(); 
     } 
    }); 
    ArrayList<Ing> listOfIngs = new ArrayList<>(); 
    IngAdapter adapter = new IngAdapter(getContext(), listOfIngs); 
    ListView listView = (ListView) root.findViewById(R.id.list); 
    listView.setAdapter(adapter); 
    return root; 

} 

適配器

public class IngAdapter extends ArrayAdapter<Ing> { 
    IngAdapter(Context context, ArrayList<Ing> ings) { 
     super(context, 0, ings); 
    } 
} 

對象

final Ing tomatoSauce = new Ing("Tomato Sauce", 0 ,0); 

final Ing chicken = new Ing("Chicken", 0 ,0); 

final Ing olives = new Ing("Olives", 0, 0); 

對象類

public class Ing { 

    int init; 

    int value; 

    String name; 

    Ing(String name, int value, int init) { 
     this.name = name; 
     this.value = value; 
     this.init = init; 
    } 
} 

佈局文件

<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.lastineindustries.ingredismartv2.Kitchen"> 

<!-- TODO: Update blank fragment layout --> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <Button 
     android:id="@+id/add" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="Add An Ingredient" 
     android:textSize="30sp"/> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 

     <ListView 
      android:id="@+id/list" 
      android:layout_width="320dp" 
      android:layout_height="match_parent"> 

     </ListView> 

     <ListView 
      android:layout_width="60dp" 
      android:layout_height="match_parent"> 
     </ListView> 

    </LinearLayout> 

</LinearLayout> 

提前任何幫助謝謝!

+0

凡'notifyDataSetChanged()'? –

回答

0

您的列表爲空。您必須添加項目到list.Try此代碼:

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    View root = inflater.inflate(R.layout.fragment_kitchen, container, false); 
    add = (Button) root.findViewById(R.id.add); 
    add.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      spinnerDialog.showSpinerDialog(); 
     } 
    }); 
    ArrayList<Ing> listOfIngs = new ArrayList<>(); 
    listOfIngs.add(new Ing("test1",1,1)); 
    listOfIngs.add(new Ing("test2",2,2)); 
    IngAdapter adapter = new IngAdapter(getContext(), listOfIngs); 

    ListView listView = (ListView) root.findViewById(R.id.list); 
    listView.setAdapter(adapter); 
    return root; 

} 
0

我想你錯過了這個代碼

listOfIngs.add(tomatoSauce); 
listOfIngs.add(chicken); 
listOfIngs.add(olives); 

只要把要初始化這些對象之後。

+0

謝謝,我知道我錯過了一些東西! – Keith

0

嘗試在IngAdapter的構造函數中添加ArrayList項。

適配器

public class IngAdapter extends ArrayAdapter<Ing> { 
    IngAdapter(Context context, ArrayList<Ing> ings) {   
     Ing tomatoSauce = new Ing("Tomato Sauce", 0 ,0); 
     Ing chicken = new Ing("Chicken", 0 ,0); 
     Ing olives = new Ing("Olives", 0, 0); 

     ings.add(tomatoSauce); 
     ings.add(chicken); 
     ings.add(olives); 

     super(context, 0, ings); 
    } 
}