Android– 从Fragment中的Firebase数据库加载数据

Android– 从Fragment中的Firebase数据库加载数据,第1张

概述我有一个使用片段的NavigationDrawer活动.在我的一个片段中,我想将数据从Firebase数据库加载到RecyclerView.所以我有一个ArrayList用于存储在数据库中的对象.然后我将ValueEventListener添加到我的数据库引用,在那里我将数据库中的对象添加到数组列表中.然后我将ArrayList提供给R

我有一个使用片段的NavigationDrawer活动.在我的一个片段中,我想将数据从Firebase数据库加载到RecyclerVIEw.

所以我有一个ArrayList用于存储在数据库中的对象.然后我将ValueEventListener添加到我的数据库引用,在那里我将数据库中的对象添加到数组列表中.然后我将ArrayList提供给RecyclerVIEw Adapter,它应该用我的对象创建列表.

当我使用addListenerForSingleValueEvent它永远不会工作时,我总是得到一个空的RecyclerVIEw.当我使用addValueEventListener时,它既不在开始时工作,但是当我将手机的方向切换为横向时,数据突然出现.如果我回到正常方向,它仍然在那里.但是当我打开片段时,不是在开始时.

这是片段类的代码:

public class LogFragment extends Fragment {    private RecyclerVIEw recyclerVIEw;    private RecyclerVIEw.Adapter rvAdapter;    private RecyclerVIEw.LayoutManager rvLayoutManager;    private ArrayList<LogEntry> entrIEs;    private DatabaseReference databaseReference;    private DatabaseReference logReference;    private FirebaseAuth firebaseAuth;    private FirebaseUser firebaseUser;    public LogFragment() {        // required empty public constructor    }    @OverrIDe    public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) {        VIEw rootVIEw = inflater.inflate(R.layout.fragment_log, container, false);        databaseReference = FirebaseDatabase.getInstance().getReference();        firebaseAuth = FirebaseAuth.getInstance();        firebaseUser = firebaseAuth.getCurrentUser();        loadEntrIEs();        recyclerVIEw = (RecyclerVIEw) rootVIEw.findVIEwByID(R.ID.recyclerVIEw);        rvLayoutManager = new linearlayoutmanager(getActivity());        recyclerVIEw.setLayoutManager(rvLayoutManager);        rvAdapter = new RvAdapter(entrIEs);        recyclerVIEw.setAdapter(rvAdapter);        // Inflate the layout for this fragment        return rootVIEw;    }    private voID loadEntrIEs(){        entrIEs = new ArrayList<>();        logReference = databaseReference.child("log").child(firebaseUser.getUID());        ValueEventListener valueEventListener = new ValueEventListener() {            @OverrIDe            public voID onDataChange(DataSnapshot dataSnapshot) {                for(DataSnapshot ds : dataSnapshot.getChildren()){                    entrIEs.add(ds.getValue(LogEntry.class));                }            }            @OverrIDe            public voID onCancelled(DatabaseError databaseError) {                Log.w("LogFragment", "loadLog:onCancelled", databaseError.toException());            }        };        logbuchreference.addValueEventListener(valueEventListener);    }}

这是我的RvAdapter.java:

public class RvAdapter extends RecyclerVIEw.Adapter<RvAdapter.MyVIEwHolder> {    private ArrayList<LogEntry> entrIEs;    public RvAdapter(ArrayList<LogEntry> entrIEs){        this.entrIEs = entrIEs;    }    @OverrIDe    public MyVIEwHolder onCreateVIEwHolder(VIEwGroup parent, int vIEwType) {        VIEw itemVIEw = LayoutInflater.from(parent.getContext()).inflate(R.layout.log_entry_item, null);        return new MyVIEwHolder(itemVIEw);    }    @OverrIDe    public voID onBindVIEwHolder(MyVIEwHolder holder, int position) {        SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");        holder.tvAction.setText(entrIEs.get(position).action);        holder.tvDate.setText(dateFormat.format(entrIEs.get(position).date)+"\n"+timeFormat.format(entrIEs.get(position).date));        holder.ivIcon.setimageResource(entrIEs.get(position).icon);        holder.itemVIEw.setonClickListener(new VIEw.OnClickListener(){            @OverrIDe            public voID onClick(VIEw v) {                // Do something            }        });    }    @OverrIDe    public int getItemCount() {        return entrIEs.size();    }    public class MyVIEwHolder extends RecyclerVIEw.VIEwHolder {        TextVIEw tvAction, tvDate;        ImageVIEw ivIcon;        public MyVIEwHolder(VIEw itemVIEw) {            super(itemVIEw);            tvAction = (TextVIEw) itemVIEw.findVIEwByID(R.ID.tvAction);            tvDate = (TextVIEw) itemVIEw.findVIEwByID(R.ID.tvDate);            ivIcon = (ImageVIEw) itemVIEw.findVIEwByID(R.ID.ivIcon);        }    }}

在这里我的LogEntry.java:

public class LogEntry {    public String action;    public Date date;    public int icon;    public String message;    public LogEntry() {}    public LogEntry(String action, Date date, int icon, String message) {        this.action = action;        this.date = date;        this.icon = icon;        this.message = message;    }}

有人知道可能是什么问题吗?

编辑:

当我System.out.prinln在for()后面的ArrayList我得到数据.因此从数据库加载数据是有效的.但是提供数据是行不通的.

解决方法:

Firebase是异步的.在回调中设置或通知适配器.

ValueEventListener valueEventListener = new ValueEventListener() {        @OverrIDe        public voID onDataChange(DataSnapshot dataSnapshot) {            for(DataSnapshot ds : dataSnapshot.getChildren()){                entrIEs.add(ds.getValue(LogEntry.class));            }            // Declare adapter and set here            // OR... adapter.notifyDataSetChanged();        }        @OverrIDe        public voID onCancelled(DatabaseError databaseError) {            Log.w("LogFragment", "loadLog:onCancelled", databaseError.toException());        }    };

更详细的,

    loadEntrIEs(); // ... Doing stuff in the background    recyclerVIEw = (RecyclerVIEw) rootVIEw.findVIEwByID(R.ID.recyclerVIEw);    rvLayoutManager = new linearlayoutmanager(getActivity());    recyclerVIEw.setLayoutManager(rvLayoutManager);    rvAdapter = new RvAdapter(entrIEs); // This is still empty, probably    recyclerVIEw.setAdapter(rvAdapter);    // entrIEs.add() called later, but adapter never notifIEd
总结

以上是内存溢出为你收集整理的Android – 从Fragment中的Firebase数据库加载数据全部内容,希望文章能够帮你解决Android – 从Fragment中的Firebase数据库加载数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1108559.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存