我創建了一個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>
提前任何幫助謝謝!
凡'notifyDataSetChanged()'? –