Firebase 在每个循环后给出空值

我有一个类Home,其中使用一种方法从 Firebase 提取数据getDatafromFirebase(),当我在循环中记录值时,它会显示该值,但是当我将其添加到列表中并更新适配器时,会RecyclerView显示null所有值。


public class Home extends Fragment {

    public Home() {}


    RecyclerView recyclerView;

    RecyclerView.LayoutManager layoutManager;

    private DatabaseReference reference = FirebaseDatabase.getInstance ().getReference ();

    ArrayList<FirebaseVideoDetials> list;

    FirebaseVideoDetials details;

    RecyclerViewAdapter adapter;

    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,

                             Bundle savedInstanceState) {

        View view  =inflater.inflate(R.layout.fragment_home, container, false);

        RelativeLayout without = view.findViewById(R.id.withoutInternet);

        RelativeLayout mainLayout = view.findViewById(R.id.mainLayout);

        recyclerView = view.findViewById(R.id.recyclerView);

        layoutManager = new LinearLayoutManager(getActivity());

        recyclerView.setLayoutManager(layoutManager);

        recyclerView.setItemAnimator(new DefaultItemAnimator());

        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),LinearLayoutManager.HORIZONTAL));

        details = new FirebaseVideoDetials();

        list = new ArrayList<FirebaseVideoDetials>();

        getDatafromFirebase();

        adapter = new RecyclerViewAdapter(getActivity(), list);

        recyclerView.setAdapter(adapter);

        if(!Internetcheck.checkConnection(getActivity())){

            without.setVisibility(View.VISIBLE);

            mainLayout.setVisibility(View.INVISIBLE);

        }else{

            without.setVisibility(View.INVISIBLE);

            mainLayout.setVisibility(View.VISIBLE);

        }

        return view;

    }



皈依舞
浏览 117回答 3
3回答

呼如林

在调用 adapter.notifyDataSetChanged 之前必须先初始化适配器adapter = new RecyclerViewAdapter(getActivity(), list);getDatafromFirebase();

翻过高山走不出你

我通过在单独的函数中调用Listview的add函数解决了这个问题:public void getDatafromFirebase(){&nbsp; &nbsp; &nbsp; &nbsp; reference.addValueEventListener(new ValueEventListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onDataChange(DataSnapshot dataSnapshot) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataSnapshot Videoreference = dataSnapshot.child("Videos");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (DataSnapshot videoschild : Videoreference.getChildren()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataSnapshot description = videoschild.child("Description");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataSnapshot duration = videoschild.child("Duration");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataSnapshot title = videoschild.child("Title");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataSnapshot thumbnail = videoschild.child("Thumbnail");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataSnapshot date = videoschild.child("Date");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataSnapshot time = videoschild.child("Time");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updatelist(description.getValue(String.class),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title.getValue(String.class),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String.valueOf(duration.getValue()),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; thumbnail.getValue(String.class),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String.valueOf(date.getValue()),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String.valueOf(time.getValue()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adapter.notifyDataSetChanged();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; progressbar.setVisibility(View.GONE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onCancelled(DatabaseError databaseError) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(getActivity(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; public void updatelist(String desc,String title,String duration,String thumbnail,String Date,String time){&nbsp; &nbsp; &nbsp; &nbsp; FirebaseVideoDetials details = new FirebaseVideoDetials();&nbsp; &nbsp; &nbsp; &nbsp; details.setDescription(desc);&nbsp; &nbsp; &nbsp; &nbsp; details.setDuration(duration);&nbsp; &nbsp; &nbsp; &nbsp; details.setTitle(title);&nbsp; &nbsp; &nbsp; &nbsp; details.setThumbnail(thumbnail);&nbsp; &nbsp; &nbsp; &nbsp; details.setTime(time);&nbsp; &nbsp; &nbsp; &nbsp; details.setDate(Date);&nbsp; &nbsp; &nbsp; &nbsp; list.add(details);&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java