它不会从Fragment Activity上的Firebase加载('FragmentOne'很明显,不应该这样)

时间:2019-08-04 19:33:11

标签: java android firebase firebase-realtime-database

我在一个Fragment上制作了一个Recycler View,将其与Firebase连线,但是它不会显示任何活动信息,也不会从Firebase加载。。我一千次验证了代码和过程,我真的不知道该怎么办,也许这是我的代码或连接中的问题。

片段类:

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();
  }
}

这是我的MainActivity类,其中包含片段:

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);
  }
}

我在调试器中没有任何错误

2 个答案:

答案 0 :(得分:1)

您制作了适配器,但未将其分配给RecyclerView。致电mRecycler.setAdapter()

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

答案 1 :(得分:0)

也尝试在onStart()或MainActivity中调用此方法:

 adapter.notifyDataSetChanged();