应用更新 firebase 实时数据库中的数据,但在 recyclerview 中不显示任何内容 [英] App updates data in firebase real-time database but doesn't show anything in recyclerview

查看:24
本文介绍了应用更新 firebase 实时数据库中的数据,但在 recyclerview 中不显示任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据添加到 firbase 实时数据库并在 recyclerview 上检索该数据.我能够成功地做到这一点但是当我尝试从 firebase 检索数据时它没有发生即使当我尝试打印我的数据时在 logacat 窗口上,我可以成功地做到这一点.

<块引用>

MainActivity.java

 import androidx.annotation.NonNull;导入 androidx.annotation.Nullable;导入 androidx.appcompat.app.AppCompatActivity;导入 androidx.recyclerview.widget.LinearLayoutManager;导入 androidx.recyclerview.widget.RecyclerView;导入 android.os.Bundle;导入 android.util.Log;导入 android.view.View;导入 android.widget.Button;导入 android.widget.EditText;导入 android.widget.Toast;导入 com.google.firebase.database.ChildEventListener;导入 com.google.firebase.database.DataSnapshot;导入 com.google.firebase.database.DatabaseError;导入 com.google.firebase.database.DatabaseReference;导入 com.google.firebase.database.FirebaseDatabase;导入 com.shivam.fireapp.model.User;导入 java.util.ArrayList;导入 java.util.List;公共类 MainActivity 扩展 AppCompatActivity {私人按钮按钮;私人按钮 button2;EditText editText;EditText editText2;字符串标签 =我的标签";私人 ChildEventListener mchildeventlistener;私有 RecyclerView mRecyclerView;私有 mModelAdapter 模型适配器;私人列表<用户>mDataList;DatabaseReference mRootRef= FirebaseDatabase.getInstance().getReference();DatabaseReference mRefMain =mRootRef.child("Users");@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) findViewById(R.id.Senddata);button2 = (Button) findViewById((R.id.GetData));editText = (EditText) findViewById(R.id.Firstname);editText2 = (EditText) findViewById(R.id.Agee);mDataList = new ArrayList<>();mRecyclerView= findViewById(R.id.recyclerView);mRecyclerView.setLayoutManager(new LinearLayoutManager(this));modelAdapter = new mModelAdapter(this,mDataList);mRecyclerView.setAdapter(modelAdapter);mchildeventlistener = 新的 ChildEventListener() {@覆盖public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {用户用户 = dataSnapshot.getValue(User.class);mDataList.add(user);modelAdapter.notifyDataSetChanged();Log.d(TAG,"OnChildAdded : Name"+ user.getFirst_name());Log.d(TAG,"OnChildAdded : ageeee"+ user.getAgeeee());}@覆盖public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {}@覆盖public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {}@覆盖public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {}@覆盖public void onCancelled(@NonNull DatabaseError databaseError) {}};mRefMain.addChildEventListener(mchildeventlistener);}@覆盖受保护的无效 onStart(){super.onStart();button.setOnClickListener(new View.OnClickListener() {@覆盖public void onClick(View v) {字符串名称 = editText.getText().toString();String Age = editText2.getText().toString();User user = new User(Name,Age);String key = mRefMain.push().getKey();mRefMain.child(key).setValue(user);Toast.makeText(MainActivity.this,"Done",Toast.LENGTH_SHORT).show();}});button2.setOnClickListener(new View.OnClickListener() {@覆盖public void onClick(View v) {}});}@覆盖受保护的无效 onDestroy(){super.onDestroy();mRefMain.removeEventListener(mchildeventlistener);}}

<块引用>

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout 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"工具:上下文=.MainActivity"><按钮android:id="@+id/发送数据"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="32dp"android:text="发送数据"应用程序:layout_constraintBottom_toBottomOf="@+id/GetData"应用程序:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="@+id/GetData"/><编辑文本android:id="@+id/名字"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginStart="32dp"android:layout_marginTop="41dp"android:layout_marginEnd="39dp"机器人:ems =10"android:hint="输入名称"android:inputType="textPersonName"app:layout_constraintEnd_toStartOf="@+id/Agee"应用程序:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"/><按钮android:id="@+id/GetData"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="160dp"android:layout_marginEnd="40dp"android:text="获取数据"应用程序:layout_constraintEnd_toEndOf="父"app:layout_constraintTop_toTopOf="parent"/><编辑文本android:id="@+id/Agee"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginTop="41dp"android:layout_marginEnd="40dp"机器人:ems =10"android:hint="进入一个时代"android:inputType="textPersonName"应用程序:layout_constraintEnd_toEndOf="父"应用程序:layout_constraintStart_toEndOf="@+id/名字"app:layout_constraintTop_toTopOf="parent"/><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/recyclerView"android:layout_width="match_parent"android:layout_height="345dp"android:layout_marginTop="55dp"应用程序:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/Senddata"/></androidx.constraintlayout.widget.ConstraintLayout>

<块引用>

mModelAdapter.java

 import androidx.annotation.NonNull;导入 androidx.recyclerview.widget.RecyclerView;导入 com.shivam.fireapp.model.User;导入 java.util.List;公共类 mModelAdapter 扩展 RecyclerView.Adapter{私有上下文上下文;私人列表<用户>mDataList;公共 mModelAdapter(上下文上下文,列表<用户> mDataList){this.context = 上下文;this.mDataList = mDataList;}@非空@覆盖public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {查看 rootView= LayoutInflater.from(context).inflate(R.layout.recycler_view_item,parent,false);返回新的 MyViewHolder(rootView);}@覆盖public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {用户 user = mDataList.get(position);holder.textView.setText(user.getFirst_name()+" | "+user.getAgeeee());}@覆盖公共 int getItemCount() {返回 mDataList.size();}公共类 MyViewHolder 扩展 RecyclerView.ViewHolder{文本视图文本视图;public MyViewHolder(@NonNull View itemView) {超级(项目视图);textView = itemView.findViewWithTag(R.id.textView);}}}

<块引用>

用户.java

 公共类用户 {字符串名字,ageeee;公共用户(字符串first_name,字符串ageeee){this.first_name = first_name;this.ageeee = ageeee;}公共用户(){}公共字符串 getFirst_name() {返回名字;}public void setFirst_name(String first_name) {this.first_name = first_name;}公共字符串 getAgeee() {返回年龄;}公共无效setAgeeee(字符串ageeee){this.ageeee = ageeee;}}

<块引用>

recycler_view_item.xml

 <?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="wrap_content"><文本视图android:id="@+id/textView"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="8dp"机器人:文本=文本视图"android:textSize="24sp"机器人:重力=中心"/></LinearLayout>

解决方案

我遇到了同样的问题.其原因是当firebase还没有从实时数据库中读取数据的时候,recyclerview已经做了;并且它还没有任何数据.为了解决这个问题,当您确定 firebase 已读取数据时,您应该制作 recyclerview.然后使用正确的数据制作recyclerview.

I am trying to add data onto firbase realtime database and retrieve that data on a recyclerview.I am successfully able to do that but when i try to retrieve data from firebase its not happening even-though when i try to print my data on logacat window i am successfully able to do that.

MainActivity.java

    import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.firebase.database.ChildEventListener;
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.shivam.fireapp.model.User;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private  Button button;
    private  Button button2;
    EditText editText;
    EditText editText2;
    String TAG ="MyTag";
    private ChildEventListener mchildeventlistener;
    private RecyclerView mRecyclerView;
    private mModelAdapter modelAdapter;
    private List<User> mDataList;

    DatabaseReference mRootRef= FirebaseDatabase.getInstance().getReference();
    DatabaseReference mRefMain =mRootRef.child("Users");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.Senddata);
        button2 = (Button) findViewById((R.id.GetData));
        editText = (EditText) findViewById(R.id.Firstname);
        editText2 = (EditText) findViewById(R.id.Agee);
        mDataList = new ArrayList<>();

        mRecyclerView= findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        modelAdapter = new mModelAdapter(this,mDataList);
        mRecyclerView.setAdapter(modelAdapter);
        mchildeventlistener = new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                User user = dataSnapshot.getValue(User.class);
                mDataList.add(user);
                modelAdapter.notifyDataSetChanged();

                Log.d(TAG,"OnChildAdded : Name    "+ user.getFirst_name());
                Log.d(TAG,"OnChildAdded : ageeee    "+ user.getAgeeee());



            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        };

        mRefMain.addChildEventListener(mchildeventlistener);





    }
    @Override
    protected void onStart(){
        super.onStart();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String Name = editText.getText().toString();
                String Age = editText2.getText().toString();

                User user = new User(Name,Age);

                String key = mRefMain.push().getKey();
                mRefMain.child(key).setValue(user);

                Toast.makeText(MainActivity.this,"Done",Toast.LENGTH_SHORT).show();
            }
        });







        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {



            }});



    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
        mRefMain.removeEventListener(mchildeventlistener);
    }


}

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/Senddata"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:text="Send Data"
        app:layout_constraintBottom_toBottomOf="@+id/GetData"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/GetData" />

    <EditText
        android:id="@+id/Firstname"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="41dp"
        android:layout_marginEnd="39dp"
        android:ems="10"
        android:hint="Enter Name"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toStartOf="@+id/Agee"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/GetData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="160dp"
        android:layout_marginEnd="40dp"
        android:text="Get Data"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/Agee"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="41dp"
        android:layout_marginEnd="40dp"
        android:ems="10"
        android:hint="Entre a Age"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/Firstname"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="345dp"
        android:layout_marginTop="55dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/Senddata" />
</androidx.constraintlayout.widget.ConstraintLayout>

mModelAdapter.java

    import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.shivam.fireapp.model.User;

import java.util.List;

public class mModelAdapter extends RecyclerView.Adapter<mModelAdapter.MyViewHolder> {
    private Context context;
    private List<User> mDataList;

    public mModelAdapter(Context context, List<User> mDataList) {
        this.context = context;
        this.mDataList = mDataList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View rootView= LayoutInflater.from(context).inflate(R.layout.recycler_view_item,parent,false);

        return new MyViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        User user = mDataList.get(position);
        holder.textView.setText(user.getFirst_name()+"  |  "+user.getAgeeee());

    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder{
        TextView textView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewWithTag(R.id.textView);
        }
    }
}

User.java

    public class User {

    String first_name, ageeee;

    public User(String first_name, String ageeee) {
        this.first_name = first_name;
        this.ageeee = ageeee;
    }

    public User() {
    }

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getAgeeee() {
        return ageeee;
    }

    public void setAgeeee(String ageeee) {
        this.ageeee = ageeee;
    }
}

recycler_view_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/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="TextView"
        android:textSize="24sp"
        android:gravity="center"/>
</LinearLayout>

解决方案

I had the same problem. Its reason is that when firebase hasn't read data from realtime database yet, recyclerview has been made; and it hasn't have any data yet. For solving that you should make recyclerview when you are sure that firebase has read data. Then recyclerview will be made with correct data.

这篇关于应用更新 firebase 实时数据库中的数据,但在 recyclerview 中不显示任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆