无法在ListView上看到我的项目

时间:2016-09-06 07:40:12

标签: android listview

Java代码:

public class BlankFragment extends Fragment {


    public BlankFragment() {}


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


        View view=inflater.inflate(R.layout.fragment_blank, container, false);
        ListView listView=(ListView) view.findViewById(R.id.listview);
        ArrayList<String> listItems=new ArrayList<String>();
        listItems.add("example");
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,listItems);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        return view;
    }

}

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="layout.BlankFragment">


        <ListView
            android:layout_width="wrap_content"
            android:layout_height="204dp"
            android:id="@+id/listview"
            android:background="#d36868"
            android:layout_gravity="center"></ListView>

    </FrameLayout>

这是我的代码...我初始化了一个ListView,我试图添加一个名为“example”的项目。当我运行这个应用程序时,我可以看到我的列表视图,但没有任何ietm。

2 个答案:

答案 0 :(得分:0)

上面的代码没有问题。因为我试过了。 enter image description here

我认为这是一些错误代码,你没有从中获取代码。如其他片段,活动等。

答案 1 :(得分:0)

尝试使用wrap_content代替固定高度

<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="layout.BlankFragment">

        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/listview"
            android:background="#d36868"
            android:layout_gravity="center"></ListView>

</FrameLayout>
相关问题