无法从firebase数据库android,recycler视图中检索数据

Posted

技术标签:

【中文标题】无法从firebase数据库android,recycler视图中检索数据【英文标题】:Unable to retrieve data from firebase database android, recycler view 【发布时间】:2021-08-05 23:01:15 【问题描述】:

模型类代码

public class DeleteEditEventsModel

    private String title, description, date, time, venue;

    DeleteEditEventsModel()  

    public DeleteEditEventsModel(String title, String description, String date, String time, String venue) 
        this.title = title;
        this.description = description;
        this.date = date;
        this.time = time;
        this.venue = venue;
    

    public String getTitle() 
        return title;
    

    public void setTitle(String title) 
        this.title = title;
    

    public String getDescription() 
        return description;
    

    public void setDescription(String description) 
        this.description = description;
    

    public String getDate() 
        return date;
    

    public void setDate(String date) 
        this.date = date;
    

    public String getTime() 
        return time;
    

    public void setTime(String time) 
        this.time = time;
    

    public String getVenue() 
        return venue;
    

    public void setVenue(String venue) 
        this.venue = venue;
    

这门课有什么问题吗?

适配器类代码:

public class DeleteEditEventsAdapter extends FirebaseRecyclerAdapter<DeleteEditEventsModel, DeleteEditEventsAdapter.myViewHolder> 

    public DeleteEditEventsAdapter(@NonNull FirebaseRecyclerOptions<DeleteEditEventsModel> options) 
        super(options);
    

    @Override
    protected void onBindViewHolder(@NonNull final myViewHolder holder, final int position, @NonNull final DeleteEditEventsModel model) 

        holder.title.setText(model.getTitle());
        holder.description.setText(model.getDescription());
        holder.date.setText(model.getDate());
        holder.time.setText(model.getTime());
        holder.venue.setText(model.getVenue());


        holder.delete_btn.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 

                FirebaseDatabase.getInstance().getReference().child("Events")
                        .child(getRef(position).getKey()).removeValue();
            
        );
    

    @NonNull
    @Override
    public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_delete_events, parent, false);
        return new myViewHolder(view);
    

    class myViewHolder extends RecyclerView.ViewHolder 

        TextView title, description, date, time, venue, edit_btn, delete_btn;


        public myViewHolder(@NonNull View itemView) 
            super(itemView);

            title = (TextView) itemView.findViewById(R.id.delete_event_Title);
            description = (TextView) itemView.findViewById(R.id.delete_event_Description);
            date = (TextView) itemView.findViewById(R.id.delete_event_Date);
            time = (TextView) itemView.findViewById(R.id.delete_event_Time);
            venue = (TextView) itemView.findViewById(R.id.delete_event_Venue);
            edit_btn = (TextView) itemView.findViewById(R.id.delete_event_edit_btn_tv);
            delete_btn = (TextView) itemView.findViewById(R.id.delete_event_btn_TV);
        
    


活动类

public class DeleteEditEventsActivity extends AppCompatActivity 

    RecyclerView recyclerView;
    DeleteEditEventsAdapter adapter;

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

        recyclerView = (RecyclerView) findViewById(R.id.delete_events_recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true);



        FirebaseRecyclerOptions<DeleteEditEventsModel> options =
                new FirebaseRecyclerOptions.Builder<DeleteEditEventsModel>()
                .setQuery(FirebaseDatabase.getInstance().getReference("Events"), DeleteEditEventsModel.class)
                .build();

        adapter = new DeleteEditEventsAdapter(options);

        recyclerView.setAdapter(adapter);
    

    @Override
    protected void onStart() 
        super.onStart();
        adapter.startListening();
    

    @Override
    protected void onStop() 
        super.onStop();
        adapter.stopListening();
    

JSON 文件


  "CSE Notification" : 
    "cse notification" : "Computer Science and Engineering"
  ,
  "Central Notification" : 
    "central notification" : "Inn-sha-ALLAH we'll be doing"
  ,
  "Civil Notification" : 
    "civil notification" : "civil"
  ,
  "ECE Notification" : 
    "ece notification" : "ece"
  ,
  "EEE Notification" : 
    "eee notification" : "eee"
  ,
  "Events" : 
    "21:00" : 
      "EventDate" : "08/05/21",
      "EventDescription" : "laptops",
      "EventTime" : "21:00",
      "EventTitle" : "Lenovo",
      "EventVenue" : "Hyderabad"
    ,
    "5:16" : 
      "EventDate" : "17/05/21",
      "EventDescription" : "To retrieve",
      "EventTime" : "5:16",
      "EventTitle" : "Trying",
      "EventVenue" : "Hyderabad"
    
  ,
  "Examination_Branch_Notification" : 
    "em_noti" : "AstagfiruILLAH"
  ,
  "IT Notification" : 
    "it notification" : "Information Technology"
  ,
  "MBA Notification" : 
    "mba notification" : "mba"
  ,
  "MECH Notification" : 
    "mech notification" : "MECH"
  ,
  "M_TECH_Notification" : 
    "mTech" : "Master of Technology"
  

我认为我的模型类有问题

As you can see Data is not being fed into the card

我第二次使用回收站视图,但我检查了我的代码与之前的代码完全相同(正如我已验证的那样),但这里无法理解问题。

【问题讨论】:

请编辑您的问题以在您的数据库中显示/Events 的数据(作为文本,请不要截图)。您可以通过单击Firebase Database console 上溢出菜单 (⠇) 中的“导出 JSON”链接来获取此信息。 请编辑您的问题并添加 Frank van Puffelen 要求的内容,也请回复 @。 @AlexMamo 编辑了问题 【参考方案1】:

当您尝试将实时数据库中的节点映射到“DeleteEditEventsModel”类型的对象时,您的类中存在的字段名称必须与数据库中存在的属性名称相匹配。不幸的是,在您的情况下,这些字段不匹配。看,您的数据库中的字段以“事件”开头,而类中的字段不是,这是正确的。

要解决这个问题,您有两个选择,您可以更改数据库中属性的名称以匹配类中的名称,或者您可以在 getter 前面使用注释。例如,如果您有一个名为“date”的字段并且数据库中的属性名为“EventDate”,那么您的 getter 应该如下所示:

@PropertyName("EventDate")
public String getDate() 
    return date;

通过这种方式,您告诉编译器查找名为“EventDate”而不是“日期”的属性。

【讨论】:

以上是关于无法从firebase数据库android,recycler视图中检索数据的主要内容,如果未能解决你的问题,请参考以下文章

无法从firebase数据库android,recycler视图中检索数据

Android - “firebase.test.lab”设置无法从 Firebase 分析中排除 Play 商店测试活动

无法从 Android 中的 Firebase 存储获取下载网址 [重复]

无法从 Android 上 Unity 中的 Firebase 快照获取“空”值

无法从 Firebase 读取用户数据

Android Studio Firebase Firestore 无法检索字段数据