ListView不在Fragment中显示数据

时间:2017-07-24 03:19:55

标签: java android listview

我正在使用片段中的类的自定义对象创建ListView,并且ListView在模拟器中是空白的,所以我不完全确定我忘记了什么。以下是onCreateView()方法,适配器,对象,对象类和布局文件。

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>

提前感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

你的清单是空的。您必须将项目添加到列表中。请尝试以下代码:

  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;

}

答案 1 :(得分:0)

我猜你错过了这段代码

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

在初始化这些对象后立即放置它。

答案 2 :(得分:0)

尝试在IngAdapter的构造函数中添加ArrayList项。

适配器

   [HttpPost]
   public ActionResult bulk_insert(IEnumerable<tabledata> data)
    {
       //How can I get table datas here

    }