我有一个FirebaseRecyclerAdapter,它的模型返回null。当我访问POJO类的模型对象时,它只返回null。我已经使用firebase-UI和firebase-quickstart示例检查了整个代码。
FirebaseRecyclerAdapter
mAdapter = new FirebaseRecyclerAdapter<EventDetails, EventViewHolder>(EventDetails.class,R.layout.event_viewholder,EventViewHolder.class,myQuery) {
@Override
protected void populateViewHolder(final EventViewHolder viewHolder,final EventDetails model,final int position) {
final DatabaseReference eventRef = getRef(position);
final String postKey = eventRef.getKey();
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Message.ts(getActivity(),"Clicked on key" + postKey);
// Intent intent = new Intent(getActivity(), PostDetailActivity.class);
// intent.putExtra("", postKey);
// startActivity(intent);
}
});
Message.ts(getActivity(),"safdfdf"+model.getEventName());
viewHolder.Name.setText(eventRef.child("eventName").child());
viewHolder.Desc.setText(model.getEventDesc());
if (model.getCoordinates()!=null){
viewHolder.Location.setVisibility(View.VISIBLE);
String[] ltlng = model.getCoordinates().split(",");
String url = "http://maps.google.com/maps/api/staticmap?center=" + ltlng[0] + "," + ltlng[1] + "&zoom=15&size=400x200&sensor=false";
viewHolder.Map.setImageUrl(url,mImageLoader);
}
}
};
mRecyclerView.setAdapter(mAdapter);
Pojo课程
public class EventDetails {
private String EventName, EventDesc;
private int limit = -1;
private String Coordinates;
public EventDetails(){}//Empty constructor for Firebase
public EventDetails(String eventName, String eventDesc) {
this.EventName = eventName;
this.EventDesc = eventDesc;
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
public String getCoordinates() {
return Coordinates;
}
public void setCoordinates(String coordinates) {
Coordinates = coordinates;
}
public String getEventName() {
return EventName;
}
public String getEventDesc() {
return EventDesc;
}
}
Viewholder
public class EventViewHolder extends RecyclerView.ViewHolder {
public TextView Name, Desc, Date;
public NetworkImageView Map;
public LinearLayout Location;
public EventViewHolder(View itemView) {
super(itemView);
Name = (TextView) itemView.findViewById(R.id.eventNameDisp);
Desc = (TextView) itemView.findViewById(R.id.eventDescDisp);
Date = (TextView) itemView.findViewById(R.id.dateDisp);
Map = (NetworkImageView) itemView.findViewById(R.id.mapNIView);
Location = (LinearLayout) itemView.findViewById(R.id.location);
}
}
Firebase-UI版本:1.01(最新) Firebase / Play服务版本:10.0.1
答案 0 :(得分:1)
通过在POJO类中添加适当的构造函数和getter以及setter方法解决了这个问题。
public class EventDetails {
private String EventName, EventDesc;
private int limit = -1;
private String Coordinates;
private String Date;
private String date;
public EventDetails() {
}//Empty constructor for Firebase
public EventDetails(String eventName, String eventDesc, String Date) {
this.EventName = eventName;
this.EventDesc = eventDesc;
this.Date = Date;
}
public EventDetails(String EventName,String EventDesc,int limit,String Coordinates,String Date){
this.EventName = EventName;
this.EventDesc = EventDesc;
this.limit = limit;
this.Coordinates = Coordinates;
this.Date = Date;
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
public String getCoordinates() {
return Coordinates;
}
public void setCoordinates(String coordinates) {
Coordinates = coordinates;
}
public String getEventName() {
return EventName;
}
public void setEventName(String eventName) {
EventName = eventName;
}
public String getEventDesc() {
return EventDesc;
}
public void setEventDesc(String eventDesc) {
EventDesc = eventDesc;
}
public String getDate() {
return date;
}
}
感谢无人帮助!!
答案 1 :(得分:0)
使模型类成员名称与firebase数据库键名相同。 如果数据库是这样的 岗位 1.PostImage 2.PostTitle 然后,模型类的成员变量将是String PostImage,String PostTitle。
答案 2 :(得分:0)
以 JavaBean 命名模式为模型类和 getter 和 setter 添加默认构造函数是必须的(https://firebaseopensource.com/projects/firebase/firebaseui-android/database/readme/),例如:-
String name;
public void setName(String name){
this.name=name
}
public String getName(){
return name;
}