android – 来自Firebase的数据未显示在RecyclerView上

android – 来自Firebase的数据未显示在RecyclerView上,第1张

概述我正在尝试显示来自Firebase数据库的数据,但我得到以下信息: my recyclerview Firebase database. 我一直在寻找不同的解决方案,但我仍然会回到同样的问题.我对Android和编程都很新,所以任何帮助都会受到赞赏.这是相关的代码. LogbookFragment: public class LogbookFragment extends Fragment { 我正在尝试显示来自Firebase数据库的数据,但我得到以下信息:
my recyclerview

Firebase database.

我一直在寻找不同的解决方案,但我仍然会回到同样的问题.我对AndroID和编程都很新,所以任何帮助都会受到赞赏.这是相关的代码.

LogbookFragment:

public class LogbookFragment extends Fragment {private TextVIEw textVIEw;private CardVIEw cardVIEw;private String assessment;private String details;private RecyclerVIEw rv;RequestQueue requestQueue;private List<Assessment> skillsList = new ArrayList<>();private static final String ARG_SECTION_NUMBER = "position";/** * Returns a new instance of this fragment for the given section * number. */public static LogbookFragment newInstance(int position) {    LogbookFragment fragment = new LogbookFragment();    Bundle args = new Bundle();    args.putInt(ARG_SECTION_NUMBER,position);    fragment.setArguments(args);    return fragment;}public LogbookFragment() {}@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) {    VIEw rootVIEw = inflater.inflate(R.layout.fragment_logbook,container,false);    Context context = getActivity();    cardVIEw = (CardVIEw) rootVIEw.findVIEwByID(R.ID.cv);    rv = (RecyclerVIEw) rootVIEw.findVIEwByID(R.ID.rv);    final linearlayoutmanager llm = new linearlayoutmanager(context);    rv.setLayoutManager(llm);    final RVAdapter adapter = new RVAdapter(skillsList);    rv.setAdapter(adapter);    initializeData();    return rootVIEw;}private voID initializeData(){    skillsList = new ArrayList<>();    Firebase ref = new Firebase("https://athenaJs.firebaseio.com/assessments");    final RVAdapter adapter = new RVAdapter(skillsList);    ref.addValueEventListener(new ValueEventListener() {        @OverrIDe        public voID onDataChange(DataSnapshot dataSnapshot) {            System.out.println(dataSnapshot.getValue());            for(DataSnapshot postsnapshot : dataSnapshot.getChildren()) {                for (int i = 0; i<42; i++) {                    assessment = postsnapshot.child("assessment").getChildren().toString();                    details = postsnapshot.child("details").getChildren().toString();                    skillsList.add(new Assessment(assessment,details));                    rv.setAdapter(adapter);                    adapter.notifyDataSetChanged();                }            }        }        @OverrIDe        public voID onCancelled(FirebaseError firebaseError) {            System.out.println("The read Failed: " + firebaseError.getMessage());        }    });}

}

RVAdapter:

public class RVAdapter extends RecyclerVIEw.Adapter<RVAdapter.AssessVIEwHolder> {public static class AssessVIEwHolder extends RecyclerVIEw.VIEwHolder {    CardVIEw cv;    TextVIEw assessname;    TextVIEw pracname;    AssessVIEwHolder(VIEw itemVIEw) {        super(itemVIEw);        cv = (CardVIEw)itemVIEw.findVIEwByID(R.ID.cv);        assessname = (TextVIEw)itemVIEw.findVIEwByID(R.ID.assessment);        pracname = (TextVIEw) itemVIEw.findVIEwByID(R.ID.details);    }}List<Assessment> skill;RVAdapter(List<Assessment> skills){    this.skill = skills;}@OverrIDepublic voID onAttachedToRecyclerVIEw(RecyclerVIEw recyclerVIEw) {    super.onAttachedToRecyclerVIEw(recyclerVIEw);}@OverrIDepublic AssessVIEwHolder onCreateVIEwHolder(VIEwGroup vIEwGroup,int i) {    VIEw v = LayoutInflater.from(vIEwGroup.getContext()).inflate(R.layout.itemlogbook,vIEwGroup,false);    AssessVIEwHolder avh = new AssessVIEwHolder(v);    return avh;}@OverrIDepublic voID onBindVIEwHolder(AssessVIEwHolder assessVIEwHolder,int i) {    assessVIEwHolder.assessname.setText(skill.get(i).assessment);    assessVIEwHolder.pracname.setText(skill.get(i).details);}@OverrIDepublic int getItemCount() {    return skill.size();}

}

评估类:

public class Assessment {String assessment;String details;Assessment(String assessment,String details) {    this.assessment = assessment;    this.details = details;}public String getAssessment() {    return assessment;}public String getDetails() {    return details;}
解决方法 正如评论中所述,问题可能是您只在初始化适配器后才将数据添加到skillsList中 – 并且由于skillsList首先是空的,因此无需显示任何内容.
因此,您实际上并未更改基础数据,而是使用第一个初始数据初始化适配器 – 这些是您应该对initializeData()方法所做的更改:

private voID initializeData(){    skillsList = new ArrayList<>();    Firebase ref = new Firebase("https://athenaJs.firebaseio.com/assessments/skills");    ref.addValueEventListener(new ValueEventListener() {        @OverrIDe        public voID onDataChange(DataSnapshot dataSnapshot) {            System.out.println(dataSnapshot.getValue());            for(DataSnapshot postsnapshot : dataSnapshot.getChildren()) {                for (int i = 0; i<42; i++) {                    assessment = postsnapshot.child("assessment").getValue().toString();                    details = postsnapshot.child("details").getValue().toString();                    //here you add the assessment object to the List                    skillsList.add(new Assessment(assessment,details));                                    }            }            //at this point you have looped through all the "children" -             //Now you are ready to initialize the adapter            RVAdapter adapter = new RVAdapter(skillsList);            rv.setAdapter(adapter));            //commented out: no need to call this Now - since you have not made any changes to the data            //adapter.notifyDataSetChanged();        }        @OverrIDe        public voID onCancelled(FirebaseError firebaseError) {            System.out.println("The read Failed: " + firebaseError.getMessage());        }    });}

我希望这有帮助.试一试,让我知道.

总结

以上是内存溢出为你收集整理的android – 来自Firebase的数据未显示在RecyclerView上全部内容,希望文章能够帮你解决android – 来自Firebase的数据未显示在RecyclerView上所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存