FirebaseRecyclerAdapter 模型 returns 空

FirebaseRecyclerAdapter model returns null

我有一个 FirebaseRecyclerAdapter,它的模型返回 null。当我访问 POJO class 的模型对象时,它 returns 仅为空。我已经用 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);

波乔class

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;

    }

}

取景器

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

问题已通过在 POJO class 中添加适当的构造函数和 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;

    }

}

谢谢大家的帮助!!

使模型 class 成员名称与 firebase 数据库密钥名称相同。 如果数据库像 Post 1.PostImage 2.PostTitle 那么modelclass的成员变量就是StringPostImage,StringPostTitle.

在 Java Bean 命名模式中为模型 class 和 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;
}