Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 除非调用getListView(),否则ListFragment不显示列表元素?_Android_Listview_Android Fragments - Fatal编程技术网

Android 除非调用getListView(),否则ListFragment不显示列表元素?

Android 除非调用getListView(),否则ListFragment不显示列表元素?,android,listview,android-fragments,Android,Listview,Android Fragments,问题在于我的onActivityCreated方法,这里我使用setListAdapter()设置适配器,但没有显示任何列表项。但是,如果我使用方法getListView()我的列表项显示!我遵循了代码,我相信listview是通过ListFragment.ensureList()在内部设置的 现在我知道为什么会发生这种情况,但我很想知道为什么需要调用getListView()——无论是添加标题还是添加单击侦听器——如果您只是显示一个列表,而不需要交互或标题呢 public class Shel

问题在于我的onActivityCreated方法,这里我使用
setListAdapter()
设置适配器,但没有显示任何列表项。但是,如果我使用方法
getListView()我的列表项显示!我遵循了代码,我相信listview是通过
ListFragment.ensureList()
在内部设置的

现在我知道为什么会发生这种情况,但我很想知道为什么需要调用
getListView()
——无论是添加标题还是添加单击侦听器——如果您只是显示一个列表,而不需要交互或标题呢

public class ShelfFragment extends ListFragment {

    // Our list view (The root view)
    private View mRoot = null;


    // Do all data initializations here
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    // container should be fragment_container
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRoot = inflater.inflate(R.layout.fragment_books, container,false);
        return mRoot;
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        TextView tv = new TextView(getActivity());
        tv.setText("List header");
        //getListView();

        String[] bookData = new String[]{
                "Book 1",
                "Book 2",
                "Book 3",
                "Book 4"
        };

        ArrayAdapter adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_activated_1, bookData);
        setListAdapter(adapter);
    }

}    
编辑:fragment_books.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    android:background="#888"
     />

我之前没有发表这篇文章的原因是因为我不想让我的问题变得复杂和庞大——但我现在明白了,我需要为你们提供更多的背景。 如何包含片段(fragment_activity.xml):


公共类FragmentActivity扩展了AppCompatActivity{
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.list_书籍);
}
}

一切正常。但由于您尚未在此处发布xml文件,所以问题只能在xml listview id中出现,请确保列表视图id为
android:id=“@android:id/list”
。listview应该位于
fragment\u books.xml

在这里,我使用了下面的xml及其与
ShelfFragment
代码的配合

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_img">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>


</LinearLayout>

更新 更改列表id后,如果您想获得更多详细信息,如如何附加片段,或在输出屏幕截图中,则需要提供更多详细信息。[![在此处输入图像描述][1]][1]

正如你在问题中补充的那样

我之前没有发布这篇文章的原因是因为我不想让我的问题变得复杂和笨重-但我现在明白了,我需要为你们提供更多的背景信息


请检查我从您的片段代码中得到了什么。

您可以发布您的
fragment\u书籍
xml文件吗?谢谢您的回复。我刚刚编辑了我的原始帖子并加入了代码。我的listview的id确实设置为该值。是必需的吗?我有id=“android:id/empty”的文本视图,还是可选的?对不起,这不是必需的。这是错的,没关系。我只是想了解为什么列表项没有显示。我刚刚添加了如何将片段包含到活动中。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_img">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>


</LinearLayout>