【问题标题】:ListView does not show contentListView 不显示内容
【发布时间】:2017-05-15 15:21:17
【问题描述】:

我尝试从firebase 检索数据并使用adapterlistview 中显示它们。 检索数据一切正常,我通过调试器进行了检查,但没有显示任何内容。 这是更新的resource_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/borders">

    <ListView
        android:id="@+id/listViewBooks"
        android:layout_height="match_parent"
        android:layout_width="match_parent" />
</RelativeLayout>

更新了 resource_item.xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/author"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

ResourceAdapter.java:

public class ResourceAdapter extends ArrayAdapter<Books> {

    private Activity context;
    private List<Books> bookList;

    public ResourceAdapter(Activity context, List<Books> bookList){
        super(context, R.layout.resource_item, bookList);
        this.context = context;
        this.bookList = bookList;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        LayoutInflater inflater = context.getLayoutInflater();
        View listViewItem = inflater.inflate(R.layout.resource_item, null, true);

        TextView textViewName = (TextView) listViewItem.findViewById(R.id.name);
        TextView textViewAuthor = (TextView) listViewItem.findViewById(R.id.author);
        TextView textViewDate = (TextView) listViewItem.findViewById(R.id.date);

        Books book = bookList.get(position);
        textViewName.setText(book.getName());
        textViewAuthor.setText(book.getAuthor());
        textViewDate.setText(String.valueOf(book.getDate()));

        return listViewItem;
    }
}

ResourceActivity.Java:

public class Resource extends AppCompatActivity {
    DatabaseReference databaseBooks;
    ListView listViewBooks;
    List<Books> bookList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.resource_layout);

        databaseBooks = FirebaseDatabase.getInstance().getReference("books");

        bookList = new ArrayList<>();
        listViewBooks = (ListView) findViewById(R.id.listViewBooks);
    }

    @Override
    protected void onStart() {
        super.onStart();

        databaseBooks.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                bookList.clear();

                for (DataSnapshot bookSnapshot : dataSnapshot.getChildren()){
                    Books book = bookSnapshot.getValue(Books.class);
                    bookList.add(book);
                }

                ResourceAdapter adapter = new ResourceAdapter(Resource.this, bookList);
                listViewBooks.setAdapter(adapter);

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }
}

【问题讨论】:

    标签: android listview firebase firebase-realtime-database adapter


    【解决方案1】:

    您的ListView 高度是0dp,它有一个重量。它不适用于RelativeLayout。就这样改吧。

    <ListView
        android:id="@+id/listViewBooks"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />
    

    更新:

    像这样更改行布局。将TextViews中的weight参数去掉,设置LinearLayout的layout_heightwrap_content。高度计算可能会导致问题。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />
    
            <TextView
                android:id="@+id/author"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />
    
            <TextView
                android:id="@+id/date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />
    
    </LinearLayout>
    

    更新 2: 尝试如下更改适配器的 getView() 方法。

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    
        if(convertView==null){
            // inflate the layout
            LayoutInflater inflater = context.getLayoutInflater();
            convertView = inflater.inflate(R.layout.resource_item, null, false);
        }
    
    
        TextView textViewName = (TextView) convertView.findViewById(R.id.name);
        TextView textViewAuthor = (TextView) convertView.findViewById(R.id.author);
        TextView textViewDate = (TextView) convertView.findViewById(R.id.date);
    
        Books book = bookList.get(position);
        textViewName.setText(book.getName());
        textViewAuthor.setText(book.getAuthor());
        textViewDate.setText(String.valueOf(book.getDate()));
    
        return convertView;
    }
    

    【讨论】:

    • 嗯,我按照你说的做了,但是布局仍然没有显示任何数据
    • @CristinaTincu 首先,您的 ListView 布局参数错误。如果仍然无法正常工作,那么还有其他问题。检查我的答案,更新。
    • 谢谢,还是不行,我已经按照你说的更新了xml文件
    • @CristinaTincu 然后检查我的更新 2,修改了你的适配器。
    • 我使用 Log.d 并看到 convertView 始终为空,在您的更新 2 中未定义 listViewItem,因此我尝试将其替换为 convertView 并尝试将 if 正文添加到以前的版本。 ..你能告诉我,总的来说,我的适配器写对了吗?
    【解决方案2】:

    问题在于您的XML。你的父布局是RelativeLayout,你正在使用android:layout_weight="1"android:layout_height="0dp"

    您可以在RelativeLayout 中使用ListView,如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/borders">
    
        <ListView
            android:id="@+id/listViewBooks"
            android:layout_height="match_parent"
            android:layout_width="match_parent" />
    
    </RelativeLayout>
    

    您也可以使用LinearLayout,如下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/borders"
        android:orientation="vertical">
    
        <ListView
            android:id="@+id/listViewBooks"
            android:layout_weight="1"
            android:layout_height="0dp"
            android:layout_width="match_parent" />
    </<LinearLayout>
    

    【讨论】:

    • 嗯,我按照你说的做了,但是布局仍然没有显示任何数据
    猜你喜欢
    • 1970-01-01
    • 2021-10-16
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多