Ad

It Doesn't Load From Firebase On The Fragment Activity ('FragmentOne' Is Clear And It Shouldn't Be)

I made a Recycler View on a Fragment , wired it with the Firebase, but it won't show anything on the activity , doesn't load from Firebase.. I verified the code and the procedure a thousand times , I really don't know what to do , maybe its a problem in my code or connections..

Fragment class :

public class FragmentUnu extends Fragment {
  View v;

  FirebaseDatabase mDatabase;
  DatabaseReference mRef;
  RecyclerView mRecycler;
  RecyclerView.LayoutManager layoutManager;
  FirebaseRecyclerAdapter < FragmentUnuModel, FragmentUnuViewHolder > adapter;

  public FragmentUnu() {}

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    v = inflater.inflate(R.layout.unu_fragment, container, false);


    mRecycler = (RecyclerView) v.findViewById(R.id.fragment_unu_recycler);
    mRecycler.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(getActivity());
    mRecycler.setLayoutManager(layoutManager);


    mDatabase = FirebaseDatabase.getInstance();
    mRef = mDatabase.getReference("FragmentId");

    FirebaseRecyclerOptions < FragmentUnuModel > options = new FirebaseRecyclerOptions.Builder < FragmentUnuModel > ()
      .setQuery(mRef, FragmentUnuModel.class)
      .build();


    adapter = new FirebaseRecyclerAdapter < FragmentUnuModel, FragmentUnuViewHolder > (options) {

      @NonNull
      @Override
      public FragmentUnuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
          .inflate(R.layout.unu_fragment_item, parent, false);
        return new FragmentUnuViewHolder(itemView);

      }

      @Override
      protected void onBindViewHolder(@NonNull FragmentUnuViewHolder fragmentUnuViewHolder, int i, @NonNull FragmentUnuModel fragmentUnuModel) {

        fragmentUnuViewHolder.denumire.setText(fragmentUnuModel.getDenumire());
        fragmentUnuViewHolder.descriere.setText(fragmentUnuModel.getDescriere());
        fragmentUnuViewHolder.pret.setText(fragmentUnuModel.getPret());
        Picasso.with(getActivity().getBaseContext()).load(fragmentUnuModel.getImagine()).into(fragmentUnuViewHolder.img);


      }
    };
    return v;
  }

  @Override
  public void onStart() {
    super.onStart();
    adapter.startListening();
    mRecycler.setAdapter(adapter);
  }

  @Override
  public void onStop() {
    super.onStop();
    adapter.stopListening();
  }
}

This is my MainActivity class with the fragments :

public class Food extends AppCompatActivity {

  private TabLayout tabLayout;
  private ViewPager viewPager;
  private ViewPagerAdapter adapter;

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

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    adapter = new ViewPagerAdapter(getSupportFragmentManager());

    adapter.AddFragment(new FragmentUnu(), "Unu");
    adapter.AddFragment(new FragmentDoi(), "Doi");
    adapter.AddFragment(new FragmentTrei(), "Trei");

    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);
  }
}

I don't have any erros in the debugger

Ad

Answer

You made the adapter but didn't assign it to RecyclerView. Call mRecycler.setAdapter():

...
mRecycler.setAdapter(adapter);
return v;

Ad
source: stackoverflow.com
Ad