如何从Firebase中的数据库中检索所有用户的详细信息 [英] How to retrieve all the user details from database in firebase

查看:168
本文介绍了如何从Firebase中的数据库中检索所有用户的详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够从Firebase中检索数据,但是只能从android中的数据库中获取单个用户。

我只想从所有数据库中获取数据用户在firebase中输入的列没有用户身份验证登录。

  import android.os.Bundle; 

导入android.support.annotation.Nullable;
导入android.support.v7.app.AppCompatActivity;
导入android.util.Log;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
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 ViewDatabase extends AppCompatActivity {
private static final String TAG =ViewDatabase;

private DatabaseReference databaseReference;
private String userID;

私人ListView mListView;

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

mListView =(ListView)findViewById(R.id.listview);
databaseReference = FirebaseDatabase.getInstance()。getReference();

databaseReference.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
showData(dataSnapshot);
}

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


$ private void showData(DataSnapshot dataSnapshot){
userinfo user = dataSnapshot.getValue(userinfo.class);

Log.d(TAG,showData:name:+ user.getName());
Log.d(TAG,showData:Mobile:+ user.getMob());
Log.d(TAG,showData:Vehicle:+ user.getVehicle());
Log.d(TAG,showData:Address:+ user.getaddress());

ArrayList< String> array = new ArrayList<>();
array.add(user.getName());
array.add(user.getMob());
array.add(user.getVehicle());
array.add(user.getaddress());
ArrayAdapter adapter = new $ b $ ArrayAdapter(this,android.R.layout.simple_list_item_1,array);
mListView.setAdapter(adapter);




$ b $ p $这些是我的userinfo类:

  public class userinfo {
public String name;
public String mob;
public String vehicle;
公共字符串地址;
$ b public public userinfo(){
}

public userinfo(String name,String mob,String vehicle,String address){
this.name =名称;
this.mob = mob;
this.vehicle =车辆;
this.address = address;
}

public String getName(){
return name;
}

public String getMob(){
return mob;
}

public String getVehicle(){
return vehicle;
}

public String getaddress(){
return address;





这是我的数据库: p>

在这里输入图像描述



我只是在列表视图中获取WjGb6At3hRdvOBBFysv65XG37k53用户

解决方案

不是很清楚。通过用户数据,如果你的意思是其他用户的 user 对象,那么简单地说,你不能获得其他用户的用户对象,而不管他们的登录方法。您只能通过 FirebaseAuth.getInstance()。getCurrentUser()方法获取您自己的用户对象。 >

话虽如此,如果用户的数据存储在实时数据库中,您可以像从其他节点获取数据一样获得它。但请注意,应用程序的隐私设置将决定您是否可以阅读其他用户的数据。



如果您在数据库中显示数据库结构这个问题。

查看数据库结构后编辑:

这是一个相当基本的问题。你需要的是一个 childEventListener 来检索 trace-5fa8c 节点的所有子节点。

这个代码如下所示: -

$ $ p $ DatabaseReference databaseReference = FirebaseDatabase.getInstance()。getReference()。child(trace-5fa8c);

databaseReference.addChildEventListener(new ChildEventListener(){
@Override $ b $ public void onChildAdded(DataSnapshot dataSnapshot,String s){
userinfo user = dataSnapshot.getValue(userinfo .class);

Log.d(TAG,showData:name:+ user.getName());
Log.d(TAG,showData:Mobile:+ user .getMob());
Log.d(TAG,showData:Vehicle:+ user.getVehicle());
Log.d(TAG,showData:Address:+ user.getaddress ());

//做任何你需要的处理

$ b @Override
public void onChildChanged(DataSnapshot dataSnapshot,String s){

$ b $ @覆盖
public void onChildRemoved(DataSnapshot dataSnapshot){

}

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

}

@Overr ide
public void onCancelled(DatabaseError databaseError){

}
};

另外,即使这不是您的问题的一部分,我想补充说每个 onChildAdded(datasnapshot),你可以得到一个 userInfo pojo。所以,在你的 showData()方法中,不要每次都创建一个新的 Arraylist $ c> Arraylist< UserInfo> 并且每次添加一个子对象时都继续添加子pojo。这个 ArrayList 对象将是您的 ListView 对象的数据集。


I am able to retrieve data from Firebase but for single user only from database in android.

I just want to get the data from database of all the with all the columns which user have entered in firebase without user authentication login.

 import android.os.Bundle;

 import android.support.annotation.Nullable;
 import android.support.v7.app.AppCompatActivity;
 import android.util.Log;
 import android.widget.ArrayAdapter;
 import android.widget.ListView;
 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 ViewDatabase extends AppCompatActivity {
     private static final String TAG = "ViewDatabase";

     private DatabaseReference databaseReference;
     private  String userID;

     private ListView mListView;

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

    mListView = (ListView) findViewById(R.id.listview);
    databaseReference = FirebaseDatabase.getInstance().getReference();

    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            showData(dataSnapshot);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

  }

  private void showData(DataSnapshot dataSnapshot) {
        userinfo  user = dataSnapshot.getValue(userinfo.class);

        Log.d(TAG, "showData: name: " + user.getName());
        Log.d(TAG, "showData: Mobile: " + user.getMob());
        Log.d(TAG, "showData: Vehicle: " + user.getVehicle());
        Log.d(TAG, "showData: Address: " + user.getaddress());

        ArrayList<String> array  = new ArrayList<>();
        array.add(user.getName());
        array.add(user.getMob());
        array.add(user.getVehicle());
        array.add(user.getaddress());
        ArrayAdapter adapter = new 
        ArrayAdapter(this,android.R.layout.simple_list_item_1,array);
        mListView.setAdapter(adapter);
    }
}

Here is my userinfo class:

public class userinfo {
   public String name;
   public String mob;
   public String vehicle;
   public String address;

   public userinfo(){
}

public userinfo(String name, String mob, String vehicle, String address) {
    this.name = name;
    this.mob = mob;
    this.vehicle = vehicle;
    this.address = address;
}

public String getName() {
    return name;
}

public String getMob() {
    return mob;
}

public String getVehicle() {
    return vehicle;
}

public String getaddress() {
    return address;
}

}

Here is my database:

enter image description here

i am just getting WjGb6At3hRdvOBBFysv65XG37k53 user in list view

解决方案

Your question isn't very clear. By user data, if you mean the user object of the other users, then simply put, you can not obtain the user object of the other users regardless of their sign-in method. You can only obtain your own user object by the FirebaseAuth.getInstance().getCurrentUser() method.

Having said that, if the data of the users is stored in the realtime database, you can obtain it in the same way that you obtain data from other nodes. Please note, though, that the privacy settings of your app will determine if you can read other users' data or not.

It would help if you show your database structure in the question.

Edit After Viewing Database Structure :-

Alright, from what I understand, this is quite a basic problem. What you need is a childEventListener to retrieve all the children ( in your case - users ) of the trace-5fa8c node.

The code for that is as follows :-

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("trace-5fa8c");

databaseReference.addChildEventListener(new ChildEventListener() {
@Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            userinfo  user = dataSnapshot.getValue(userinfo.class);

            Log.d(TAG, "showData: name: " + user.getName());
            Log.d(TAG, "showData: Mobile: " + user.getMob());
            Log.d(TAG, "showData: Vehicle: " + user.getVehicle());
            Log.d(TAG, "showData: Address: " + user.getaddress());

            //Do whatever further processing you need
        }

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

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

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

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    };

Also, even though this is not a part of your question, I'd like to add that with each onChildAdded(datasnapshot), you can get a userInfo pojo. So, in your showData() method, instead of creating a new Arraylist each time, maintain a single Arraylist<UserInfo> and keep adding the child pojo to it each time a child is added. This ArrayList object will be the dataset for your ListView object.

这篇关于如何从Firebase中的数据库中检索所有用户的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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