Parth Tiwari
Parth Tiwari

Reputation: 486

How to retrieve all the user details from database in firebase

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

Upvotes: 0

Views: 3899

Answers (1)

Rohan Stark
Rohan Stark

Reputation: 2396

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.

Upvotes: 3

Related Questions