firebase recycler adapter显示空行

时间:2017-10-08 13:52:05

标签: android firebase firebase-realtime-database android-recyclerview firebaseui

我正在使用firebase数据库ui 3.0.0。它工作正常并加载数据,但问题是,当我滚动recylerview时,它显示空行。

这是我的firebase回收器适配器..

 FirebaseRecyclerOptions<PostData> options =
            new FirebaseRecyclerOptions.Builder<PostData>()
                    .setQuery(boardref.child("posts"), PostData.class)
                    .setLifecycleOwner(this)
                    .build();

    adapter = new FirebaseRecyclerAdapter<PostData, blogviewholder>(options){

        @Override
        public blogviewholder onCreateViewHolder(ViewGroup parent, int viewType) {
            switch (viewType) {
                case R.layout.post_row1:
                    return new blogviewholder(LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.post_row1, parent, false));
                case R.layout.post_row2:
                    return new blogviewholder(LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.post_row2, parent, false));
                case R.layout.post_row3:
                    return new blogviewholder(LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.post_row3, parent, false));
                default:
                    throw new IllegalStateException();
            }
        }


        @Override
        protected void onBindViewHolder(blogviewholder viewHolder, int position, PostData model) {
            int view=viewHolder.getItemViewType();
            switch (view) {
                case R.layout.post_row1:
                    viewHolder.setUser(model.getUid(),getApplicationContext(),model.getTimestamp());
                    viewHolder.setDesc(model.getDesc());
                    break;
                case R.layout.post_row2:
                    viewHolder.setUser(model.getUid(),getApplicationContext(),model.getTimestamp());
                    viewHolder.setDesc(model.getDesc());
                    viewHolder.setImage(getApplicationContext(),model.getData());
                    break;
                case R.layout.post_row3:
                    viewHolder.setUser(model.getUid(),getApplicationContext(),model.getTimestamp());
                    viewHolder.setDesc(model.getDesc());
                    viewHolder.setFile(model.getData(),model.getName(),getApplicationContext());
                    break;

            }
        }

        @Override
        public int getItemViewType(int position) {
            PostData postData=getItem(position);
            String str=postData.getType();
            switch (str) {
                case "text":
                    return R.layout.post_row1;

                case "file":
                    return R.layout.post_row3;

                case "image":
                    return R.layout.post_row2;
            }
            return position;
        }


        @Override
        public void onDataChanged() {
            super.onDataChanged();
            adapter.notifyDataSetChanged();
        }
    };
    recyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(BoardDetail.this);
    mLayoutManager.setReverseLayout(true);
    mLayoutManager.setStackFromEnd(true);
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.addItemDecoration(
            new DividerItemDecoration(ContextCompat.getDrawable(getApplicationContext(),
                    R.drawable.item_decorator)));
    recyclerView.setAdapter(adapter);

这是我的博客视图持有者类

private static class blogviewholder extends RecyclerView.ViewHolder{
    View mView;
    public blogviewholder(View itemView) {
        super(itemView);
        mView=itemView;
    }

    public void setUser(String uid, final Context ctx, long  timestamp) {
        final TextView textView=(TextView) mView.findViewById(R.id.user_name);
        final TextView date=(TextView) mView.findViewById(R.id.date);
        final ImageView user_image=(ImageView) mView.findViewById(R.id.user_img);
        String str=convertTime(timestamp);
        date.setText(str);

        DatabaseReference mref=FirebaseDatabase.getInstance().getReference("users/"+uid);
        mref.keepSynced(true);
        mref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()){
                    String name=dataSnapshot.child("name").getValue().toString();
                    textView.setText(name);
                    final String image=dataSnapshot.child("profile_image").getValue().toString();
                    Picasso.with(ctx)
                            .load(image)
                            .networkPolicy(NetworkPolicy.OFFLINE)
                            .into(user_image,new Callback() {
                                @Override
                                public void onSuccess() {

                                }

                                @Override
                                public void onError() {
                                    //Try again online if cache failed
                                    Picasso.with(ctx).load(image).into((user_image));
                                }
                            });

                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }

    public void setDesc(String desc) {
        TextView mdesc=(TextView) mView.findViewById(R.id.desc);
        mdesc.setText(desc);
    }

    public void setFile(final String data, String name, final Context ctx) {

        TextView file_name= (TextView) mView.findViewById(R.id.file_name);
        ImageView file_image= (ImageView) mView.findViewById(R.id.file_bt);
        file_name.setText(name);
        file_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri intentUri = Uri.parse(data);
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setData(intentUri);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                ctx.startActivity(intent);
            }
        });



    }

    public void setImage(final Context ctx, final String data) {
        final ImageView imageView=(ImageView) mView.findViewById(R.id.post_img);
        Picasso.with(ctx)
                .load(data)
                .networkPolicy(NetworkPolicy.OFFLINE)
                .into(imageView,new Callback() {
                    @Override
                    public void onSuccess() {

                    }

                    @Override
                    public void onError() {
                        //Try again online if cache failed
                        Picasso.with(ctx).load(data).into((imageView));
                    }
                });

    }
}

这是我的模特课

public class PostData {

private String name;
private String data;
private String uid;
private String desc;
private String type;
private long timestamp;

public PostData() {
}

public PostData(String name, String data, String uid, String desc, String type, long timestamp) {
    this.name = name;
    this.data = data;
    this.uid = uid;
    this.desc = desc;
    this.type = type;
    this.timestamp = timestamp;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getData() {
    return data;
}

public void setData(String data) {
    this.data = data;
}

public String getUid() {
    return uid;
}

public void setUid(String uid) {
    this.uid = uid;
}

public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}


public long getTimestamp() {
    return timestamp;
}

public void setTimestamp(long timestamp) {
    this.timestamp = timestamp;
}

}

我的数据库结构有类型字段,它可以是图像,文件或文本,根据它,有三种布局。 这是应用程序的截图..

initial stage

after scrolling

如果我找不到解决方案,我的项目将无法完成。请帮助!!

这是我的日志..

10-09 09:45:56.720 8702-441/com.yashnagda.kkb D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=MainActivity, firebase_previous_id(_pi)=7800829491751886780, firebase_screen_class(_sc)=BoardDetail, firebase_screen_id(_si)=7800829491751886794}]
10-09 09:45:56.728 8702-8702/com.yashnagda.kkb D/ActivityThreadInjector: clearCachedDrawables.
10-09 09:45:56.797 8702-8702/com.yashnagda.kkb W/ClassMapper: No setter/field for key found on class com.yashnagda.kkb.PostData
10-09 09:45:56.809 8702-8702/com.yashnagda.kkb W/ClassMapper: No setter/field for key found on class com.yashnagda.kkb.PostData
10-09 09:45:56.823 8702-8702/com.yashnagda.kkb W/ClassMapper: No setter/field for key found on class com.yashnagda.kkb.PostData
10-09 09:45:56.835 8702-8702/com.yashnagda.kkb W/ClassMapper: No setter/field for key found on class com.yashnagda.kkb.PostData
10-09 09:45:56.846 8702-8702/com.yashnagda.kkb W/ClassMapper: No setter/field for key found on class com.yashnagda.kkb.PostData
10-09 09:45:56.858 8702-8702/com.yashnagda.kkb W/ClassMapper: No setter/field for key found on class com.yashnagda.kkb.PostData
10-09 09:45:56.904 8702-9067/com.yashnagda.kkb D/OpenGLRenderer: endAllStagingAnimators on 0x55922c46e0 (RippleDrawable) with handle 0x5592cd6e50
10-09 09:45:56.905 8702-9067/com.yashnagda.kkb D/OpenGLRenderer: endAllStagingAnimators on 0x5592789e00 (LinearLayout) with handle 0x5592f7a610
10-09 09:45:58.808 8702-8702/com.yashnagda.kkb W/ClassMapper: No setter/field for key found on class com.yashnagda.kkb.PostData
10-09 09:45:58.897 8702-8702/com.yashnagda.kkb W/ClassMapper: No setter/field for key found on class com.yashnagda.kkb.PostData
10-09 09:45:59.132 8702-8702/com.yashnagda.kkb W/ClassMapper: No setter/field for key found on class com.yashnagda.kkb.PostData
10-09 09:45:59.382 8702-8702/com.yashnagda.kkb W/ClassMapper: No setter/field for key found on class com.yashnagda.kkb.PostData
10-09 09:46:01.780 8702-441/com.yashnagda.kkb V/FA: Inactivity, disconnecting from the service

1 个答案:

答案 0 :(得分:0)

我认为您的值为空,请尝试此操作以确认您的数据Log.i("DataSnapTest",""+dataSnapshot.toString());

这将帮助您查看所有即将到来的数据,检查所有值是否正确。

希望这会对你有所帮助。