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
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