[英]Unable to retrieve data from firebase database android, recycler view
Model Class Code Model Class 代码
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;
}
}
Is there any issue with this class?这个 class 有什么问题吗?
Adapter Class Code:适配器 Class 代码:
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);
}
}
}
Activity class活动 class
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 FILE 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"
}
}
I think there is a problem in my model class我认为我的 model class 有问题
As you can see Data is not being fed into the card如您所见,数据未送入卡中
I am using recycler view for the second time but I've checked my code against the previous code it is exactly the same(As I've validated), but can't understand the problem here.我第二次使用回收器视图,但我已经检查了我的代码与以前的代码完全相同(正如我已经验证的那样),但这里无法理解问题。
When you try to map a node from the Realtime Database into an object of type "DeleteEditEventsModel", the name of the fields that exist in your class must match the name of your properties that exist in your database.当您尝试将实时数据库中的节点 map 插入“DeleteEditEventsModel”类型的 object 时,class 中存在的字段名称必须与数据库中存在的属性名称匹配Unfortunately, in your case, the fields don't match.不幸的是,在您的情况下,这些字段不匹配。 See, the fields in your database start with "Event", while in the class are not, and this is not correct.看,数据库中的字段以“事件”开头,而在 class 中不是,这是不正确的。
To solve this, you have two options, you either change the name of your properties in the database to match the one in the class, or you can use an annotation in front of the getters.为了解决这个问题,您有两个选择,您可以更改数据库中的属性名称以匹配 class 中的名称,或者您可以在 getter 前面使用注释。 For example, if you have a field called "date" and the property in the database is called "EventDate", your getter should look like this:例如,如果您有一个名为“date”的字段并且数据库中的属性名为“EventDate”,那么您的 getter 应该如下所示:
@PropertyName("EventDate")
public String getDate() {
return date;
}
In this way, you tell the compiler to look for a property called "EventDate" and not "date".通过这种方式,您告诉编译器查找名为“EventDate”而不是“日期”的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.