1

my oncreate on fragment tab :

 @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.tab1_frag, container, false);
    Bundle bundle = this.getArguments();
    if (bundle != null) {
        class_name = bundle.getString("tab1");
    }
    recyclerView = view.findViewById(R.id.recyclerview_tab1);
    recyclerViewAdepter = new RecyclerViewAdepter(getContext(), list);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));
    recyclerView.setAdapter(recyclerViewAdepter);

    class_name = "oops";
    Log.d("tag6", class_name);
    return view;


}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    mapFromDB = new HashMap<String, String>();
    Log.d("tag1", class_name);
    db = FirebaseDatabase.getInstance().getReference().child("CLASS").child("123").child("day1");
    db.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.d("tag2", "ok");
            for (DataSnapshot dbclass : dataSnapshot.getChildren()) {
                mapFromDB.put(dbclass.getKey().toString(), dbclass.getValue().toString());
                Log.d("tag3", "ok");
            }
            recyclerViewAdepter.set_map(mapFromDB);
            check = "123";
            Log.d("tag4", check);
            adding();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

            check = "456";
            adding();

        }
    });


}

public void sendToDB()

{
    db = FirebaseDatabase.getInstance().getReference().child("CLASS").child(class_name).child("day1");
    db.setValue(recyclerViewAdepter.get_map());

}


void adding() {
    Log.d("tag5", check);
    list = new ArrayList<>();
    list.add(new days_fragment(ourdata.Hours[0], check));
    list.add(new days_fragment(ourdata.Hours[1]));
    list.add(new days_fragment(ourdata.Hours[2]));
    list.add(new days_fragment(ourdata.Hours[3]));
    list.add(new days_fragment(ourdata.Hours[4]));
    list.add(new days_fragment(ourdata.Hours[5]));
    list.add(new days_fragment(ourdata.Hours[6]));
    list.add(new days_fragment(ourdata.Hours[7]));
    list.add(new days_fragment(ourdata.Hours[8]));
    list.add(new days_fragment(ourdata.Hours[9]));
    list.add(new days_fragment(ourdata.Hours[10]));
    list.add(new days_fragment(ourdata.Hours[11]));
    list.add(new days_fragment(ourdata.Hours[12]));

}

and the logcat result are :

com.example.project D/tag1: tt

com.example.project D/tag5: 000

com.example.project D/tag6: oops

com.example.project D/tag2: ok

com.example.project D/tag3: ok

com.example.project D/tag4: 123

tag 5&6 comes before 2,3,4 why this is happing and how to resolve it ?

Rahul Singh Chandrabhan
  • 2,531
  • 5
  • 22
  • 33
  • Firebase is asynchronous. – André Kool May 09 '18 at 07:50
  • I recommend you see the last part of my anwser from this **[post](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774)** and also take a look at this **[video](https://www.youtube.com/watch?v=OvDZVV5CbQg)**. – Alex Mamo May 09 '18 at 08:25
  • i understand that. but how i make that part do it before others? the variable "check" changes on db.addValueEventListener(new ValueEventListener() and if other code run before that, check wont be right – notfromhere May 09 '18 at 09:46

1 Answers1

0

Firebase event listeners work asynchronous. Retrieving data on onCreateView method must solve your issue. This will make your code more unreadable and complicated to understand but until now this is the solution I could apply and get a result. Please let me know if you find a better solution.

Nope
  • 111
  • 2
  • 12