Firebase 回收视图在 Android Studio 中不显示数据,仅显示空白暗白色背景

问题描述 投票:0回答:1

我遵循 this 在 android studio 中构建聊天应用程序的教程系列,我构建了一个附有 Firebase 数据库的回收视图,但它没有显示。您可以在this image,中看到,它仅以暗白色显示回收视图的区域,而不显示列表。

这是我的 Firebase 数据库视图: Firebase database structure

实时数据库图像Realtime-database image

身份验证选项卡图像 Authentication data image

// In MainActivity
DatabaseReference reference = database.getReference().child("User"); 


// in Register.java
    String id = task.getResult().getUser().getUid();
    DatabaseReference reference = database.getReference().child("user").child(id);
    StorageReference storageReference = storage.getReference().child("upload").child(id);

这里是:MainActivity.java:存在回收视图的地方

package com.example.javamassenger;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import com.google.firebase.Firebase;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

FirebaseAuth auth;
RecyclerView UserRecyclerView;
userAdapter adapter;
FirebaseDatabase database;
ArrayList<Users> arrayList;

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

    database = FirebaseDatabase.getInstance();
    
    DatabaseReference reference = database.getReference().child("User");
    auth = FirebaseAuth.getInstance();


    arrayList = new ArrayList<>();
   
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {

            for(DataSnapshot dataSnapshot: snapshot.getChildren())
            {
                Users users = dataSnapshot.getValue(Users.class);
                // adding data in array
                arrayList.add(users);
                Toast.makeText(MainActivity.this, "It's working", Toast.LENGTH_SHORT).show();
            }

            adapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            Toast.makeText(MainActivity.this, "Can't Access Data", Toast.LENGTH_SHORT).show();
        }
    });

    
    UserRecyclerView = findViewById(R.id.mainUserRecyclerView);
    UserRecyclerView.setLayoutManager(new LinearLayoutManager(this));

    adapter = new userAdapter(MainActivity.this, arrayList);
    UserRecyclerView.setAdapter(adapter);

}
}

这里是:适配器作为 userAdapter.java

package com.example.javamassenger;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.recyclerview.widget.RecyclerView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import de.hdodenhof.circleimageview.CircleImageView;

public class userAdapter extends RecyclerView.Adapter<userAdapter.viewholder>{

Context mainActivity;
ArrayList<Users> arrayList;

public userAdapter(MainActivity mainActivity, ArrayList<Users> arrayList) {
    this.mainActivity = mainActivity;
    this.arrayList = arrayList;
}

@NonNull
@Override
public userAdapter.viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(mainActivity).inflate(R.layout.users_item, parent,false);
    return new viewholder(view);
}


@Override
public void onBindViewHolder(@NonNull userAdapter.viewholder holder, int position) {


    Users users = arrayList.get(position);
    holder.username.setText(String.valueOf(users.userName));
    holder.status.setText(String.valueOf(users.status));
    Picasso.get().load(users.profilepic).into(holder.circleImageView);
}


@Override
public int getItemCount() {
    return arrayList.size();
}


public class viewholder extends RecyclerView.ViewHolder {

    CircleImageView circleImageView;
    TextView status, username;

    public viewholder(@NonNull View itemView) {
        super(itemView);

        circleImageView = itemView.findViewById(R.id.cirImg);
        status = itemView.findViewById(R.id.status);
        username = itemView.findViewById(R.id.username);

    }
}
}

数组的 Setters 和 Geters 类:Users.java

public class Users {
String profilepic, mail, userName, password, userId, lastMessage, status;

public Users(){}
public Users(String userId, String userName, String mail, String password, String profilepic, String status) {
    this.userId = userId;
    this.userName = userName;
    this.mail = mail;
    this.password = password;
    this.profilepic = profilepic;
    this.status = status;
}

public String getProfilepic() {
    return profilepic;
}

public void setProfilepic(String profilepic) {
    this.profilepic = profilepic;
}

public String getMail() {
    return mail;
}

public void setMail(String mail) {
    this.mail = mail;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getUserId() {
    return userId;
}

public void setUserId(String userId) {
    this.userId = userId;
}

public String getLastMessage() {
    return lastMessage;
}

public void setLastMessage(String lastMessage) {
    this.lastMessage = lastMessage;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}
}

MainActivity XML 文件activity_main.xml

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

<LinearLayout
    android:id="@+id/toolBar"
    android:layout_width="wrap_content"
    android:layout_height="@dimen/_55sdp"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_marginStart="@dimen/_23sdp"
    android:layout_marginLeft="@dimen/_22sdp"
    android:layout_marginTop="@dimen/_12sdp"
    android:layout_marginEnd="@dimen/_26sdp"
    android:layout_marginRight="@dimen/_25sdp"
    android:background="@color/white"
    android:gravity="right">

</LinearLayout>

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/mainUserRecyclerView"
    android:layout_width="334dp"
    android:layout_height="330dp"
    android:layout_below="@id/toolBar"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="13dp"
    android:layout_marginTop="160dp"
    android:layout_marginEnd="12dp"
    android:layout_marginBottom="107dp" />

users_items.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="wrap_content"
android:background="@drawable/bordermain"
android:layout_margin="13.00dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/cirImg"
        android:layout_width="114dp"
        android:layout_height="80dp"
        android:layout_marginStart="13.00dp"
        android:layout_marginTop="13.00dp"
        android:layout_marginEnd="13.00dp"
        android:layout_marginBottom="@dimen/_10sdp"
        android:src="@drawable/photocamera" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center">

        <TextView
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/poppins_bold"
            android:text="Name"
            android:textColor="@color/black"
            android:textSize="23.40dp"/>
        
        <TextView
            android:id="@+id/status"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:fontFamily="@font/poppins_regular"
            android:textSize="10.40dp"
            android:text="Status"/>

    </LinearLayout>

</LinearLayout>

</RelativeLayout>

如果还有其他需要正确理解问题的问题,请询问。如有任何帮助,我们将不胜感激。

java android firebase android-recyclerview
1个回答
0
投票

您正在更新数组列表,该列表作为 MainActivity.java 中的变量存在。适配器不会注意到该更新。要么在适配器内创建一个函数,该函数获取更新的数组列表并更新列表(对于适配器来说是本地的),然后调用notifyDataSetChanged。 或者每次更新 arrayList 时重新初始化适配器,这不是更好的方法。我建议使用第一种方法。

© www.soinside.com 2019 - 2024. All rights reserved.