侦听器仅将列表中的最后一张图像添加到RecyclerView

时间:2018-12-18 12:52:32

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

我获得了用户ID的数据快照。这些ID也与用户匹配的个人资料图片的图像ID相同。 我想用与RecyclerView匹配的每个图像填充RecyclerView,但是发生的是,它填充了正确的用户ID,但只填充了数据快照列表中的最后一个图像,而不是全部。因此,我最终得到了一个用户ID列表,并在底部有一个图像:

    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                imageName = ds.getKey();                   
                user = new User(imageName);
                myDataset.add(user);

                storageReference.child("profileImages").child(imageName).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {


                        user.setImageUri(uri);
                        // Got the download URL                            
                        mAdapter.notifyDataSetChanged();

                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception exception) {
                        // Handle any errors                            
                    }
                });
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };
    imagesRef.addListenerForSingleValueEvent(eventListener);

外观如下:

Only one image

这是适配器:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private ArrayList<User> mDataset;
    private MyViewHolder myHolder;
    private User user;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class MyViewHolder extends RecyclerView.ViewHolder {

        public TextView userIdTextView;
        public ImageView userProfileImageView;
        public View layout;
        public MyViewHolder(View v) {
            super(v);
            layout = v;
            userProfileImageView = (ImageView) v.findViewById(R.id.profile_image);

        public TextView userIdTextView = (TextView) v.findViewById(R.id.user_id_text_view);
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(ArrayList<User> myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                     int viewType) {


        View v =  LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recycler_view, parent, false);

        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        myHolder = holder;
        user = mDataset.get(position);
        Uri userImage = user.getImageUri();
        myHolder.userIdTextView.setText(user.getUsername());

        Glide.with(myHolder.itemView.getContext() /* context */)
                .load(userImage)
                .apply(new RequestOptions().override(300, 300))
                .into(myHolder.userProfileImageView);


    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.size();
    }

}

数据库结构:

database
   |_____users
           |___uid1 
           |___uid2
           |___uid3

2 个答案:

答案 0 :(得分:1)

不能在“ onSuccess()”回调方法中以这种方式使用您的“用户”变量。该变量始终引用最后的“ new User()”,因此,当“下载”过程到达“ onSuccess()”时,“ user”值的值是最新执行的“ new User()”。您需要将类似“ userID”的内容传递给下载方法,并且在执行“ onSuccess()”时,您必须使用该ID找到正确的用户,然后设置其映像。

答案 1 :(得分:0)

由于 getDownloadUrl 是一种异步方法,因此其他用户对象被更改,并且在调用 onSuccess()时仅引用了最后一个对象,因此设置了最后一个对象uri。

  1. 使用HashMap<String,User> userMap = new HashMap<String,User>();

  2. getDownloadUrl

    之前

    userMap.put(imageName,user);

  3. onSuccess()

    User user = userMap.get(imageName); user.setImageUri(uri);

使imageName为最终名称。