当前位置: 首页 > 知识库问答 >
问题:

无法从firebase数据库加载新活动中firebase回收器视图适配器上的数据

司马璞
2023-03-14

这是mainactivity代码

 package com.example.sharma.digimenu;


        import android.content.DialogInterface;
        import android.content.Intent;
        import android.support.annotation.NonNull;
        import android.support.design.widget.Snackbar;
        import android.support.v7.app.AlertDialog;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.text.TextUtils;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.widget.Button;
        import android.widget.RelativeLayout;

        import com.example.sharma.digimenu.Common.Common;
        import com.example.sharma.digimenu.Model.User;
        import com.google.android.gms.tasks.OnFailureListener;
        import com.google.android.gms.tasks.OnSuccessListener;
        import com.google.firebase.auth.AuthResult;
        import com.google.firebase.auth.FirebaseAuth;
        import com.google.firebase.database.DatabaseReference;
        import com.google.firebase.database.FirebaseDatabase;
        import com.rengwuxian.materialedittext.MaterialEditText;

        import java.util.Objects;


public class MainActivity extends AppCompatActivity {

    Button btnSignIn, btnSignUp;
    RelativeLayout rootLayout;
    FirebaseAuth auth;
    FirebaseDatabase db;
    DatabaseReference users;

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

        auth = FirebaseAuth.getInstance();
        db = FirebaseDatabase.getInstance();
        users = db.getReference("Users");

        btnSignIn = (Button) findViewById(R.id.btnSignIn);
        btnSignUp = (Button) findViewById(R.id.btnSignUp);
        rootLayout = (RelativeLayout) findViewById(R.id.rootLayout);

        btnSignUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSignUpDialog();
            }
        });

        btnSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSignInDialog();
            }
        });

    }

    private void showSignUpDialog() {
        final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("SIGN UP");
        dialog.setMessage("Please use email to sign up");
        LayoutInflater inflater = LayoutInflater.from(this);
        View signup_layout = inflater.inflate(R.layout.layout_signup, null);

        final MaterialEditText edtEmail = signup_layout.findViewById(R.id.edtEmail);
        final MaterialEditText edtPassword = signup_layout.findViewById(R.id.edtPassword);
        final MaterialEditText edtName = signup_layout.findViewById(R.id.edtName);
        final MaterialEditText edtPhone = signup_layout.findViewById(R.id.edtPhone);

        dialog.setView(signup_layout);

        dialog.setPositiveButton("SIGN UP", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                dialogInterface.dismiss();

                if (TextUtils.isEmpty(edtEmail.getText().toString())) {
                    Snackbar.make(rootLayout, "Please enter email address", Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(edtPhone.getText().toString())) {
                    Snackbar.make(rootLayout, "Please enter phone number", Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(edtPassword.getText().toString())) {
                    Snackbar.make(rootLayout, "Please enter  password", Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if (edtPassword.getText().toString().length() < 6) {
                    Snackbar.make(rootLayout, "Password too short!!!", Snackbar.LENGTH_SHORT).show();
                    return;
                }

                auth.createUserWithEmailAndPassword(edtEmail.getText().toString(), edtPassword.getText().toString())
                        .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
                            @Override
                            public void onSuccess(AuthResult authResult) {
                                User user = new User();
                                user.setEmail(edtEmail.getText().toString());
                                user.setPassword(edtPassword.getText().toString());
                                user.setPhone(edtPhone.getText().toString());
                                user.setName(edtName.getText().toString());
                                users.child(Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid()).setValue(user)
                                        .addOnSuccessListener(new OnSuccessListener<Void>() {
                                            @Override
                                            public void onSuccess(Void aVoid) {
                                                Snackbar.make(rootLayout, "Register Successfully", Snackbar.LENGTH_SHORT)
                                                        .show();
                                            }
                                        })
                                        .addOnFailureListener(new OnFailureListener() {
                                            @Override
                                            public void onFailure(@NonNull Exception e) {
                                                Snackbar.make(rootLayout, "Failed" + e.getMessage(), Snackbar.LENGTH_SHORT)
                                                        .show();
                                            }
                                        })
                        .addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Snackbar.make(rootLayout, "Failed" + e.getMessage(), Snackbar.LENGTH_SHORT)
                                        .show();
                            }
                        });
            }
        });

    }
});
        dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                dialogInterface.dismiss();
            }
        });
        dialog.show();
    }
    private void showSignInDialog() {

        final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("SIGN IN");
        dialog.setMessage("Please use email to sign in");

        LayoutInflater inflater = LayoutInflater.from(this);
        View signin_layout = inflater.inflate(R.layout.layout_signin, null);

        final MaterialEditText edtEmail = signin_layout.findViewById(R.id.edtEmail);
        final MaterialEditText edtPassword = signin_layout.findViewById(R.id.edtPassword);

        dialog.setView(signin_layout);

        dialog.setPositiveButton("SIGN IN", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                dialogInterface.dismiss();

                if (TextUtils.isEmpty(edtEmail.getText().toString())) {
                    Snackbar.make(rootLayout, "Please enter email address", Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(edtPassword.getText().toString())) {
                    Snackbar.make(rootLayout, "Please enter  password", Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if (edtPassword.getText().toString().length() < 6) {
                    Snackbar.make(rootLayout, "Password too short!!!", Snackbar.LENGTH_SHORT).show();
                    return;
                }
        auth.signInWithEmailAndPassword(edtEmail.getText().toString(), edtPassword.getText().toString())
                .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
                    public User user;

                    @Override
                    public void onSuccess(AuthResult authResult) {
                        startActivity(new Intent(MainActivity.this, Home.class));
                        Common.currentUser = user;
                        finish();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Snackbar.make(rootLayout, "Failed" + e.getMessage(), Snackbar.LENGTH_SHORT).show();
            }
        });
    }
});
        dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        dialog.show();
    }
}

这是Home.java

 package com.example.sharma.digimenu;


        import android.content.DialogInterface;
        import android.content.Intent;
        import android.support.annotation.NonNull;
        import android.support.design.widget.Snackbar;
        import android.support.v7.app.AlertDialog;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.text.TextUtils;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.widget.Button;
        import android.widget.RelativeLayout;

        import com.example.sharma.digimenu.Common.Common;
        import com.example.sharma.digimenu.Model.User;
        import com.google.android.gms.tasks.OnFailureListener;
        import com.google.android.gms.tasks.OnSuccessListener;
        import com.google.firebase.auth.AuthResult;
        import com.google.firebase.auth.FirebaseAuth;
        import com.google.firebase.database.DatabaseReference;
        import com.google.firebase.database.FirebaseDatabase;
        import com.rengwuxian.materialedittext.MaterialEditText;

        import java.util.Objects;


public class MainActivity extends AppCompatActivity {

    Button btnSignIn, btnSignUp;
    RelativeLayout rootLayout;
    FirebaseAuth auth;
    FirebaseDatabase db;
    DatabaseReference users;

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

        auth = FirebaseAuth.getInstance();
        db = FirebaseDatabase.getInstance();
        users = db.getReference("Users");

        btnSignIn = (Button) findViewById(R.id.btnSignIn);
        btnSignUp = (Button) findViewById(R.id.btnSignUp);
        rootLayout = (RelativeLayout) findViewById(R.id.rootLayout);

        btnSignUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSignUpDialog();
            }
        });

        btnSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSignInDialog();
            }
        });

    }

    private void showSignUpDialog() {
        final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("SIGN UP");
        dialog.setMessage("Please use email to sign up");
        LayoutInflater inflater = LayoutInflater.from(this);
        View signup_layout = inflater.inflate(R.layout.layout_signup, null);

        final MaterialEditText edtEmail = signup_layout.findViewById(R.id.edtEmail);
        final MaterialEditText edtPassword = signup_layout.findViewById(R.id.edtPassword);
        final MaterialEditText edtName = signup_layout.findViewById(R.id.edtName);
        final MaterialEditText edtPhone = signup_layout.findViewById(R.id.edtPhone);

        dialog.setView(signup_layout);

        dialog.setPositiveButton("SIGN UP", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                dialogInterface.dismiss();

                if (TextUtils.isEmpty(edtEmail.getText().toString())) {
                    Snackbar.make(rootLayout, "Please enter email address", Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(edtPhone.getText().toString())) {
                    Snackbar.make(rootLayout, "Please enter phone number", Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(edtPassword.getText().toString())) {
                    Snackbar.make(rootLayout, "Please enter  password", Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if (edtPassword.getText().toString().length() < 6) {
                    Snackbar.make(rootLayout, "Password too short!!!", Snackbar.LENGTH_SHORT).show();
                    return;
                }

                auth.createUserWithEmailAndPassword(edtEmail.getText().toString(), edtPassword.getText().toString())
                        .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
                            @Override
                            public void onSuccess(AuthResult authResult) {
                                User user = new User();
                                user.setEmail(edtEmail.getText().toString());
                                user.setPassword(edtPassword.getText().toString());
                                user.setPhone(edtPhone.getText().toString());
                                user.setName(edtName.getText().toString());
                                users.child(Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid()).setValue(user)
                                        .addOnSuccessListener(new OnSuccessListener<Void>() {
                                            @Override
                                            public void onSuccess(Void aVoid) {
                                                Snackbar.make(rootLayout, "Register Successfully", Snackbar.LENGTH_SHORT)
                                                        .show();
                                            }
                                        })
                                        .addOnFailureListener(new OnFailureListener() {
                                            @Override
                                            public void onFailure(@NonNull Exception e) {
                                                Snackbar.make(rootLayout, "Failed" + e.getMessage(), Snackbar.LENGTH_SHORT)
                                                        .show();
                                            }
                                        })
                        .addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Snackbar.make(rootLayout, "Failed" + e.getMessage(), Snackbar.LENGTH_SHORT)
                                        .show();
                            }
                        });
            }
        });

    }
});
        dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                dialogInterface.dismiss();
            }
        });
        dialog.show();
    }
    private void showSignInDialog() {

        final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("SIGN IN");
        dialog.setMessage("Please use email to sign in");

        LayoutInflater inflater = LayoutInflater.from(this);
        View signin_layout = inflater.inflate(R.layout.layout_signin, null);

        final MaterialEditText edtEmail = signin_layout.findViewById(R.id.edtEmail);
        final MaterialEditText edtPassword = signin_layout.findViewById(R.id.edtPassword);

        dialog.setView(signin_layout);

        dialog.setPositiveButton("SIGN IN", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                dialogInterface.dismiss();

                if (TextUtils.isEmpty(edtEmail.getText().toString())) {
                    Snackbar.make(rootLayout, "Please enter email address", Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(edtPassword.getText().toString())) {
                    Snackbar.make(rootLayout, "Please enter  password", Snackbar.LENGTH_SHORT).show();
                    return;
                }
                if (edtPassword.getText().toString().length() < 6) {
                    Snackbar.make(rootLayout, "Password too short!!!", Snackbar.LENGTH_SHORT).show();
                    return;
                }
        auth.signInWithEmailAndPassword(edtEmail.getText().toString(), edtPassword.getText().toString())
                .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
                    public User user;

                    @Override
                    public void onSuccess(AuthResult authResult) {
                        startActivity(new Intent(MainActivity.this, Home.class));
                        Common.currentUser = user;
                        finish();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Snackbar.make(rootLayout, "Failed" + e.getMessage(), Snackbar.LENGTH_SHORT).show();
            }
        });
    }
});
        dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        dialog.show();
    }
}

menuviewholder.java文件

    package com.example.sharma.digimenu.ViewHolder;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.sharma.digimenu.Interface.ItemClickListener;
import com.example.sharma.digimenu.R;

public class MenuViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    public TextView txtMenuName;
    public ImageView imageView;

    private ItemClickListener itemClickListener;

    public MenuViewHolder(View itemView) {

        super(itemView);
        txtMenuName = itemView.findViewById(R.id.menu_name);
         imageView = itemView.findViewById(R.id.menu_image);
    }
    public void setItemClickListener(ItemClickListener itemClickListener){
        this.itemClickListener = itemClickListener;
    }
    @Override
    public void onClick (View view) {
        itemClickListener.onClick(view,getAdapterPosition(),false);
    }
}
    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@drawable/background"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:background="@android:color/background_dark"
        app:itemTextColor="@android:color/background_light"
        app:itemIconTint="@android:color/background_light"
        app:headerLayout="@layout/nav_header_home"
        app:menu="@menu/activity_home_drawer" />

</android.support.v4.widget.DrawerLayout>
06-24 22:06:08.063 466-466/com.example.sharma.digimenu W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
06-24 22:06:08.063 466-466/com.example.sharma.digimenu W/BiChannelGoogleApi: [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzal@1da9cec4
06-24 22:06:08.069 466-1271/com.example.sharma.digimenu W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
06-24 22:06:08.074 466-1271/com.example.sharma.digimenu I/FirebaseAuth: [FirebaseAuth:] Loading module via FirebaseOptions.
    [FirebaseAuth:] Preparing to create service connection to gms implementation
06-24 22:06:10.323 466-585/com.example.sharma.digimenu D/FirebaseAuth: Notifying id token listeners about user ( ythvbsqhYURD3NrN5LlNtXZsDMC3 ).
06-24 22:06:10.324 466-466/com.example.sharma.digimenu D/FirebaseApp: Notifying auth state listeners.
06-24 22:06:10.325 466-466/com.example.sharma.digimenu D/FirebaseApp: Notified 1 auth state listeners.
06-24 22:06:10.328 466-466/com.example.sharma.digimenu I/Timeline: Timeline: Activity_launch_request id:com.example.sharma.digimenu time:25840099
06-24 22:06:10.337 466-521/com.example.sharma.digimenu V/FA: Recording user engagement, ms: 25151
06-24 22:06:10.338 466-521/com.example.sharma.digimenu V/FA: Connecting to remote service
06-24 22:06:10.340 466-521/com.example.sharma.digimenu V/FA: Activity paused, time: 72826503
06-24 22:06:10.344 466-521/com.example.sharma.digimenu D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=25151, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=-2677346009036919789}]
06-24 22:06:10.347 466-466/com.example.sharma.digimenu V/FA: onActivityCreated
06-24 22:06:10.372 466-521/com.example.sharma.digimenu V/FA: Connection attempt already in progress
06-24 22:06:10.474 466-521/com.example.sharma.digimenu D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=MainActivity, firebase_previous_id(_pi)=-2677346009036919789, firebase_screen_class(_sc)=Home, firebase_screen_id(_si)=-2677346009036919788}]
06-24 22:06:10.489 466-521/com.example.sharma.digimenu V/FA: Connection attempt already in progress
    Connection attempt already in progress
    Activity resumed, time: 72826637
06-24 22:06:10.544 466-521/com.example.sharma.digimenu D/FA: Connected to remote service
06-24 22:06:10.544 466-521/com.example.sharma.digimenu V/FA: Processing queued up service tasks: 4
06-24 22:06:10.617 466-466/com.example.sharma.digimenu I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@d97ab3e time:25840388
06-24 22:06:15.575 466-521/com.example.sharma.digimenu V/FA: Inactivity, disconnecting from the service

共有1个答案

楮庆
2023-03-14

你需要一个适配器。官方的firebase库没有任何适配器类,但有一个非常有用的库称为FirebaseUI。

https://github.com/firebase/firebaseui-Android

 类似资料:
  • 谁能给我一个例子hoe把保存的数据从活动传递到回收器适配器。我知道如何使用putExtra等将数据从回收器适配器传递到活动。 好心帮帮我。百万先谢! 活动A中的代码(Add address按钮可用并且存在回收器视图) 请检查我更新的回收器适配器。当我运行时,0将显示在文本区域中,如所附图像所示。我对Android系统是新手。请以身作则。

  • 因此,我尝试使用Recolyer视图和cursoradapter实现我的数据库,但我得到了错误。我不能很好地理解它们,所以我不知道是我没有正确地实现适配器,还是它是其他的东西。以下是java文件: mainactivity.java: 以下是回收器视图适配器: 最后是我得到的错误:

  • 事件适配器 事件活动 @override public void onBindViewHolder(@nonnull ViewHolder holder,final int position){

  • 我刚开始在firebase工作。我设法上传了文本和图像,但是,我无法检索要显示在回收器视图中的图像,只能检索文本。我用的是毕加索依赖。我已经包括了我的主要活动。java类,该类负责显示从问题“我的适配器”中的firebase检索的回收器视图项。java类和模型类。我相信,在我将图像URI上载到firebase存储时,我可能犯了没有存储图像URI的错误,因此适配器无法检索图像位置。我想这可能是因为我

  • 我无法填充Firebase回收适配器。我最终想做的是,用户搜索一个名称,数据库中的结果将填充回收人员视图。 数据库结构如下所示: 要填充的代码如下所示,全部位于: 现在我只想让它填充数据库中的所有用户,但似乎什么都没有填充。 是一个。 这是UsersViewHolder类:公共静态类UsersViewHolder扩展了RecyclerView。视图持有者{View mView;TextView m

  • 我想补充一点 我正在添加新的消息,下面的代码是我从Firebase文档中学到的。 我的模型是: } 和firebase侦听器 RecyclerView适配器: 很抱歉读了这么长的帖子,谢谢你抽出时间。