1

I have to create a project that has the number of Tabs, decided based on the number of children in firebase database.

I need the Number of Tabs to reduce and increase, if a new child is deleted or added to the database respectively.

I have managed to get the Number of children in the Firebase Database, but i am unable to pass it to the function that decides the Number of Tabs (getcount()).

The Class from where i am retrieving the Firebase Data:

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

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;

/**
 * A simple {@link Fragment} subclass.
 */
public class CommunitiesFragment extends Fragment {
    private static int commonVariable;
    private DatabaseReference mDatabaseReference;


    public CommunitiesFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v=inflater.inflate(R.layout.community_tab, container, false);
        mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("COMMUNITIES");
            Log.e("TESTApp", "The value is checked");
            mDatabaseReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    commonVariable = (int) dataSnapshot.getChildrenCount();
                    Log.e("COMMON-VALUE", commonVariable + "");

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });

            return v;
    }

     static int tabNumber(){
        int a = CommunitiesFragment.commonVariable;
        Log.e("Tab Function Value:",a+"");
        return a;
    }

}

The Tab Adapter Class that is responsible for the creation of the Tabs:

import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;


public class CommunityAdapter extends FragmentStatePagerAdapter {
    public CommunityAdapter(@NonNull FragmentManager fm) {
        super(fm,BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
    }


    @NonNull
    @Override
    public Fragment getItem(int position) {
        CommunitiesFragment communitiesFragment=new CommunitiesFragment();
        Bundle bundle=new Bundle();
        position=position+1;
        bundle.putString("message","Success"+position);
        communitiesFragment.setArguments(bundle);

        return communitiesFragment;
    }

    @Override
    public int getCount(){
        int TabNumber = CommunitiesFragment.tabNumber();
        Log.e("InGetCountFunction",TabNumber+"");
        return TabNumber;
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        position=position+1;
        return "COMMUNITY "+position ;
    }

    public class CommunityViewHolder {

    }

}

Image of the Firebase Database

Please Refer if needed

I want the Number of tabs to be equal to the number of children of communities. The Next step would be to display the Nested Attributes of each child in their individual Tabs

Presently The TabNumber fumction does not deliver the correct value and it just shows 0. I also feel that it may be a case that the onDataChanege function of the firebase is not getting triggered on the app startup

The Logcat for of the Error:

    2019-12-22 20:13:43.670 1408-1460/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
2019-12-22 20:13:52.354 7275-7275/com.testapp E/Tab Function Value:: 0
2019-12-22 20:13:52.354 7275-7275/com.testapp E/InGetCountFunction: 0
2019-12-22 20:13:52.355 7275-7275/com.testapp E/Tab Function Value:: 0
2019-12-22 20:13:52.355 7275-7275/com.testapp E/InGetCountFunction: 0
2019-12-22 20:13:52.362 7275-7275/com.testapp E/Tab Function Value:: 0
2019-12-22 20:13:52.362 7275-7275/com.testapp E/InGetCountFunction: 0
2019-12-22 20:13:52.364 7275-7275/com.testapp E/Tab Function Value:: 0
2019-12-22 20:13:52.364 7275-7275/com.testapp E/InGetCountFunction: 0
2019-12-22 20:13:52.412 7275-7275/com.testapp E/Tab Function Value:: 0
2019-12-22 20:13:52.412 7275-7275/com.testapp E/InGetCountFunction: 0
2019-12-22 20:13:54.119 7275-7275/com.testapp E/Tab Function Value:: 0
2019-12-22 20:13:54.119 7275-7275/com.testapp E/InGetCountFunction: 0
2019-12-22 20:13:54.783 7275-7275/com.testapp E/Tab Function Value:: 0
2019-12-22 20:13:54.783 7275-7275/com.testapp E/InGetCountFunction: 0
2019-12-22 20:13:54.971 1595-1608/? E/memtrack: Couldn't load memtrack module
2019-12-22 20:13:55.265 7275-7275/com.testapp E/Tab Function Value:: 0
2019-12-22 20:13:55.265 7275-7275/com.testapp E/InGetCountFunction: 0
2019-12-22 20:13:55.799 7275-7275/com.testapp E/Tab Function Value:: 0
2019-12-22 20:13:55.799 7275-7275/com.testapp E/InGetCountFunction: 0
2019-12-22 20:13:56.263 7275-7275/com.testapp E/Tab Function Value:: 0
2019-12-22 20:13:56.263 7275-7275/com.testapp E/InGetCountFunction: 0
2019-12-22 20:14:04.765 1595-1608/? E/memtrack: Couldn't load memtrack module
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
AgnellusX1
  • 19
  • 1

1 Answers1

0

Data is loaded from Firebase asynchronously, since it may take some time to get the data from the server. Instead of blocking the app while waiting for the data, your main code continues to run, and (for example) initializes the fragment. At this point your onDataChange hasn't run yet, and your commonVariable is still set to its default value. Only once the data is available, your onDataChange is called, and commonVariable is set to the correct value. But at this point the fragment has already rendered with the incorrect value of commonVariable.

The solution is to put any code that needs data from the database inside onDataChange or to call it from there. For an example of how to do both, see getContactsFromFirebase() method return an empty list

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks @Frank van Puffelen for the Explanation, finally understood what was going wrong, However, I tried but i am still not sure, how one can add the code to populate a Tab view inside `onDataChange`, refereed the link too, but the code and use case there is quite different from what i need. Would really appreciate if you could help me out, a bit new at this, Thanks anyway – AgnellusX1 Jan 04 '20 at 19:59
  • In your `onDataChange` you'll want to refresh the fragment, because it depends on the `commonVariable` that you're changing. A quick search hints that you might want to call `notifyDataSetChanged` on it: https://developer.android.com/reference/android/support/v4/view/PagerAdapter.html#notifyDataSetChanged() – Frank van Puffelen Jan 04 '20 at 22:40