-1

Hey people :) I am very new to programming and just got my head around using RecyclerView in Android Studio.

Now I would like to retreive data from a position x in firebase:

So for example my Database includes:

Users: Max (with the fields: University, Age, City) Lena (with the fields: University, Age, City)

I don't know how many users there are and whats their name and I'd like to get all the data of one User at position x.

Is there anybody who has an idea how to solve that problem? Thanks in advance!

EDIT: I now saved the UID from the User and passed it to the other activity. Then I am trying to receive the belonging data from firebase, but I am still not able to show the data from the Field "university" in the TextView.

public class nextActivity extends AppCompatActivity{

String UID;
private DocumentReference myReference;

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

//get data from intent (I checked the value of UID - getIntent worked.)
UID = getIntent().getStringExtra("uid");

// I am not sure with the following line. Is it possible to just add "UID"?
myReference = FirebaseFirestore.getInstance().document("Users/"+UID);
myReference.addSnapshotListener(new EventListener<DocumentSnapshot>() {
    @Override
    public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
        if (documentSnapshot.exists()) {

            long universityID = documentSnapshot.getLong("university");
            cUniversityText.setText("University"+Long.toString(universityID));

        } else if (e != null) {
            Log.w("InspiringQuote", "Exception!", e);
        }
    }
});
cUniversityText = findViewById(R.id.cuniversitytext);

}

dunkiero
  • 81
  • 7
  • The way you structured your database is not appropriate. For guidance refer - https://stackoverflow.com/questions/16421179/whats-the-best-way-of-structuring-data-on-firebase – Raj Sep 09 '18 at 16:15
  • What do you mean through `at position x`? Please responde with @. – Alex Mamo Sep 10 '18 at 12:09
  • @AlexMamo That the position depends on the Item clicked before. For example: Max is at position 0, Lena at position 1. In an activity I want to retrieve the data of position 1 to get the Data of Lena. – dunkiero Sep 10 '18 at 12:21
  • @dunkiero Are you using Firebase-UI library? – Alex Mamo Sep 10 '18 at 12:45
  • @AlexMamo Yes. The App is starting and I can click an item of the recycler view. But then on the so started activity I can't get my head around how to receive actual realtime data from firebase based on the item clicked. I was able to pass the data from the first activity to the second, but not to get data from firebase directly. I hope that helps as explanation of my problem. – dunkiero Sep 10 '18 at 13:28
  • As I used String cUniversityText = getItem(position).getUniversity(); inside of the FirebaseRecyclerAdapter to get the RecyclerView, I thought I could save the "position" and pass it to the next activity to be able to receive the data again directly from firebase. – dunkiero Sep 10 '18 at 13:36
  • 1
    @dunkiero Maybe you should save the `uid` of the user and pass it to the other activity in order to be able to get the data of a particular user. – Alex Mamo Sep 10 '18 at 13:52
  • @AlexMamo I followed your recommendation but I am still unable to see the desired data. I posted my code below. Maybe you are able to find the mistake? Thanks so far for all your help! – dunkiero Sep 10 '18 at 15:04
  • 1
    @dunkiero What does this line of code print `Log.d("TAG", String.valueOf(universityID));` when is placed right after this line `cUniversityText.setText("University"+Long.toString(universityID));`? – Alex Mamo Sep 10 '18 at 15:36
  • @AlexMamo Right now it's just saying: "To enable debug logging run: adb shell setprop log.tag.FA VERBOSE" and "I/FA: Tag Manager is not found and thus will not be used". I will try to understand and solve this problem and contact you afterwords. – dunkiero Sep 10 '18 at 17:34
  • 1
    @dunkiero Ok, keep me posted. – Alex Mamo Sep 10 '18 at 17:39
  • @AlexMamo It took me till now to understand the problem - it seems, that my mobile phone is not able - even with the secret options - to allow logging. I now used a global variabel instead and checked it afterwords by using TextView. There is no value assigned to the global variabel. Even like this: GV.setForTest(5); it stays with the value 0. So I suppose, there is no DocumentSnapshot taken at all. – dunkiero Sep 11 '18 at 14:01
  • @AlexMamo Hey! I could resolve the problem somehow - though no idea where the mistake was. Thanks for your help and time! – dunkiero Sep 11 '18 at 16:47
  • @dunkiero You're welcome, cheers! – Alex Mamo Sep 11 '18 at 16:56

1 Answers1

1

I think this might help you:

reference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        DataSnapshot users = dataSnapshot.child("users");
        int count=0;
        for (DataSnapshot usrchild : users.getChildren()){
           count++;
           if(count == 'Position x variable'){
              DataSnapshot university = usrchild.child("University");
              DataSnapshot age = usrchild.child("Age");
              DataSnapshot City = usrchild.child("City");
              if(!String.valueof(university.getValue()).matches("")){
              //Value is not null
              }
           }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Toast.makeText(getActivity(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
    }
});
Pratham Khurana
  • 141
  • 1
  • 12
  • Alright! Thanks, I got it. Now I need to set the university as a text. How can I avoide that it gets null? – dunkiero Sep 09 '18 at 22:02