0

I am trying to get recieverUID from a certain table and then get the Value of username cooresponding the UID from another table. What I think is happening is that the Adapter is being set before the receivedNames Array List has been fetched as the App runs fine when I set the receivedName Array list as a constant. Please tell me what's wrong here and if this is the correct way to do this Here's what my database looks like, Firebase DB

My code for Tab 3 Fragment

public class Tab3 extends Fragment {

private static RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private static RecyclerView recyclerView;
private static ArrayList<String> reminderMessages;
protected static ArrayList<String> receiverNames;
String senderUID;
private DatabaseReference mDatabase;
//Overriden method onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


    //Change R.layout.tab1 in you classes
    View v = inflater.inflate(R.layout.tab3, container, false);
    //get current user
    final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    reminderMessages = new ArrayList<>();
    receiverNames = new ArrayList<>();


    senderUID = user.getUid();
    mDatabase = FirebaseDatabase.getInstance().getReference();
    Query query = mDatabase.child("reminders").orderByChild("senderUID").equalTo(senderUID);

    //Setting size of recycler view as constant
    recyclerView = (RecyclerView) v.findViewById(R.id.my_recycler_view);
    recyclerView.setHasFixedSize(true);

    //Setting Linear Layout
    layoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());

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

            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                //Getting message from database
                String message = ds.child("reminderMessage").getValue(String.class);
                //Adding database to ArrayList
                reminderMessages.add(message);
                //Getting ReceiverUID
                String rUID = ds.child("receiverUID").getValue(String.class);
                //Getting corresponding username of the ReceiverUID
                GetName getName = new GetName();
                getName.GetName(rUID);
                reminderMessages.add(message);
               // Log.d("String names", receiverNames.toString());
            }


            adapter = new DataAdapter(reminderMessages,receiverNames);
            recyclerView.setAdapter(adapter);

        }


        @Override
        public void onCancelled(DatabaseError databaseError) {
            //Error in Reaching Database
            Toast.makeText(getContext(), "Something went Wrong!", Toast.LENGTH_SHORT).show();
        }


    });
    //Returning the layout file after inflating
    return v;
}

And here is my GetName Class

public class GetName {
private FirebaseAuth auth;
ListView listView;
private DatabaseReference mDatabase;
String receiverName;
public void GetName(String UID){
    mDatabase = FirebaseDatabase.getInstance().getReference().child("users");

    Query query = mDatabase.orderByKey().equalTo(UID);
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot2) {

            // dataSnapshot is the "issue" node with all children with id 0
            for (DataSnapshot users : dataSnapshot2.getChildren()) {
                receiverName = users.child("username").getValue(String.class);

                receiverNames.add(receiverName);
                Log.d("name retrieved",receiverNames.toString());

            }


        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });




}
shivam dhuria
  • 69
  • 1
  • 9

1 Answers1

1

To solve this problem, please add the declaration of your receiverNames ArrayList inside the onDataChange() method, otherwise it will be null. This is happening due to asynchronous behaviour of that method. If you want to use those values outside that method, please see my answer from this post.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193