如何从Firebase检索信息

时间:2018-10-11 10:39:48

标签: android firebase firebase-realtime-database

如何从管理器表中检索信息?

package com.example.edward.eventmanagementsystem.StartUpPage;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.edward.eventmanagementsystem.R;
import com.example.edward.eventmanagementsystem.StartUpPage.Organizer;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    //private Button button;
    Button loginButton;
    EditText editLogin, editPass;
    TextView registerLink;
    FirebaseUser user;
    String uid;
    List<String> itemList;
    DatabaseReference databaseReference;

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

        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();

        editLogin = findViewById(R.id.editLogin);
        editPass = findViewById(R.id.editPass);
        loginButton = (Button)findViewById(R.id.loginButton);
        registerLink = (TextView)findViewById(R.id.registerLink);

        user = FirebaseAuth.getInstance().getCurrentUser();
        uid = user.getUid();
        itemList = new ArrayList<>();


        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent MainMenu =  new Intent(MainActivity.this, com.example.edward.eventmanagementsystem.MainMenuPage.MainMenu.class);
                startActivity(MainMenu);
            }
        });

        registerLink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent Register = new Intent(MainActivity.this,com.example.edward.eventmanagementsystem.StartUpPage.Register.class );
                startActivity(Register);
            }
        });




        // Init Database
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        final DatabaseReference table_user = database.getReference().child("Organizer").push(); //("Organizer");

        loginButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick (View view){

                final ProgressDialog mDialog = new ProgressDialog(MainActivity.this);
                mDialog.setMessage("Please waiting...");
                mDialog.show();

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

                        //check if user does't exists in database
                        if(dataSnapshot.child(editLogin.getText().toString()).exists()) {
                            //get organizer sign in information
                            mDialog.dismiss();
                            Organizer organizer = dataSnapshot.child(editLogin.getText().toString()).getValue(Organizer.class);

                            if(organizer.getPassword().equals(editPass.getText().toString())){
                                Toast.makeText(MainActivity.this, "Sign in successfully!", Toast.LENGTH_SHORT).show();
                                Intent MainMenu =  new Intent(MainActivity.this, com.example.edward.eventmanagementsystem.MainMenuPage.MainMenu.class);
                                startActivity(MainMenu);
                            }
                            else{
                                Toast.makeText(MainActivity.this, "Sign in failed!", Toast.LENGTH_SHORT).show();
                            }
                        }
                        else
                        {
                            Toast.makeText(MainActivity.this, "User not exists in database.", Toast.LENGTH_SHORT).show();
                        }


                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }
        });

    }
}

我在从Firebase检索数据时遇到了问题。如何从Firebase检索用户登录信息。

我看到了其他人的帖子,但仍然无法成功发布。

1 个答案:

答案 0 :(得分:0)

似乎您已经使用

初始化了FirebaseUser
user = FirebaseAuth.getInstance().getCurrentUser();

如果通过了正确的身份验证,则可以使用各种方法获取user数据,这些方法可以使用来调用

user.getEmail();
user.getDisplayName();