Java Android:将ListView设置为导航抽屉

Java Android:将ListView设置为导航抽屉,java,android,mobile,android-listview,navigation-drawer,Java,Android,Mobile,Android Listview,Navigation Drawer,这就是我想要的: 使用我从服务器请求的数据填充ListView 将ListView填充到导航抽屉中 我已经用我请求的数据填充了ListView,它不是空的。但是,我无法让它将ListView填充到导航抽屉中 ListView commentsList = (ListView) findViewById(R.id.right_drawer); CommentsAdapter adapter = new CommentsAdapter(HomeCategoryActivity.this, R.lay

这就是我想要的:

  • 使用我从服务器请求的数据填充ListView
  • 将ListView填充到导航抽屉中
  • 我已经用我请求的数据填充了ListView,它不是空的。但是,我无法让它将ListView填充到导航抽屉中

    ListView commentsList = (ListView) findViewById(R.id.right_drawer);
    CommentsAdapter adapter = new CommentsAdapter(HomeCategoryActivity.this, R.layout.comment_single, comments, fname, lname);
    commentsList.setAdapter(adapter);
    
    上述代码表示:

    在主布局中是XML:

    <ListView
    android:id="@+id/right_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:background="#2B2B2B"
    android:choiceMode="singleChoice" />
    
    其中:

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout_cat"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="activity.classes.HomeCategoryActivity">
    
    
    
    最后,我不知道如何将ListView附加到抽屉


    我做错了什么?我认为将列表视图分配给抽屉不需要适配器?

    您应该将
    列表视图
    作为
    抽屉布局的第二个子项
    第一个子项将是抽屉在屏幕外时的内容,第二个子项将是抽屉在屏幕上时可见的内容(即抽屉本身)

    此外,通过这样做:

    commentsList.setAdapter(adapter_right);
    
    您正在重置列表视图上的适配器(数据源),因此您将看不到最初使用
    CommentsAdapter设置的数据


    请参见上的示例xml文件。

    抽屉布局的第二个子项是否必须是
    列表视图的类型!
    
    commentsList.setAdapter(adapter_right);