从Firebase获取图片网址和用户名,并显示到Textview和Imageview中

时间:2018-04-17 17:47:08

标签: java android firebase firebase-realtime-database android-arrayadapter

我有一个BlogActivity,其中包含个人资料图片占位符,用户名占位符,其中我使用了回收适配器来填充每个用户图片和名称。

我创建了第二个活动,其中有一个ImageViewTextView,其ID为user_imageuser_name

如何在第二项活动中显示当前user_image和当前user_name

博客活动的回收适配器是



public class BlogRecyclerAdapter extends RecyclerView.Adapter<BlogRecyclerAdapter.ViewHolder> {

    public List<BlogPost> blog_list;
    public Context context;

    private FirebaseFirestore firebaseFirestore;
    private FirebaseAuth firebaseAuth;
    private PopupWindow popWindow;

    public BlogRecyclerAdapter(List<BlogPost> blog_list){

        this.blog_list = blog_list;


    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.blog_list_item, parent, false);
        context = parent.getContext();
        firebaseFirestore = FirebaseFirestore.getInstance();
        firebaseAuth = FirebaseAuth.getInstance();
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {

        holder.setIsRecyclable(false);

        final String blogPostId = blog_list.get(position).BlogPostId;
        final String currentUserId = firebaseAuth.getCurrentUser().getUid();

        String desc_data = blog_list.get(position).getDesc();
        holder.setDescText(desc_data);

        String image_url = blog_list.get(position).getImage_url();
        String thumbUri = blog_list.get(position).getImage_thumb();
        holder.setBlogImage(image_url, thumbUri);

        String user_id = blog_list.get(position).getUser_id();
        //User Data will be retrieved here...



        firebaseFirestore.collection("Users").document(user_id).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {

                if(task.isSuccessful()){

                    String userName = task.getResult().getString("name");
                    String userImage = task.getResult().getString("image");

                    holder.setUserData(userName, userImage);


                } else {

                    //Firebase Exception

                }

            }
        });




    @Override
    public int getItemCount() {
        return blog_list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private View mView;
        private TextView blogUserName;
        private CircleImageView blogUserImage;


        public ViewHolder(View itemView) {
            super(itemView);
            mView = itemView;

            blogLikeBtn = mView.findViewById(R.id.blog_like_btn);
            blogCommentBtn = mView.findViewById(R.id.blog_comment_icon);

        }


        public void setUserData(final String name, final String image) {
            blogUserImage = mView.findViewById(R.id.blog_user_image);
            blogUserName = mView.findViewById(R.id.blog_user_name);

            blogUserName.setText(name);

            RequestOptions placeholderOption = new RequestOptions();
            placeholderOption.placeholder(R.drawable.profile_placeholder);

            if (!((Activity) context).isFinishing()) {
                Glide.with(context).applyDefaultRequestOptions(placeholderOption).load(image).into(blogUserImage);
            }

            mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, MainActivity.class);
                    intent.putExtra("user_name", name);
                    intent.putExtra("user_image", image);
                    context.startActivity(intent);
                }
            });
        }



        public void updateLikesCount(int count){

            blogLikeCount = mView.findViewById(R.id.blog_like_count);
            blogLikeCount.setText(count + " Likes");

        }

    }



}
&#13;
&#13;
&#13;

我的第二项活动是

&#13;
&#13;
        final String name = getIntent().getStringExtra("user_name");

        Toast.makeText(getBaseContext(), "You're Offline!"+name, Toast.LENGTH_SHORT).show();
&#13;
&#13;
&#13;

但是在第二项活动中name会引发错误,我无法解决。

请建议您如何将用于回收适配器的user_imageuser_name转换为任何其他活动TextViewImageView

下面是我的Firebase结构图片

Firebase Structure

提前致谢。

2 个答案:

答案 0 :(得分:0)

试试这个

final String currentUserId = firebaseAuth.getCurrentUser().getUid();
FirebaseFirestore  db = FirebaseFirestore.getInstance();
DocumentReference docRef = db.collection("Users").document(currentUserId);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                Log.d("check logcat", "DocumentSnapshot data: " + document.getData());
            } else {
                Log.d("", "No such document");
            }
        } else {
            Log.d("", "get failed with ", task.getException());
        }
    }
});

这里是doc:https://firebase.google.com/docs/firestore/query-data/get-data

答案 1 :(得分:0)

如果我理解正确,您只需将用户名和图像传递给使用implementation "com.algolia:instantsearch-android:1.7.24" 功能设置的第二个活动。因此,您只需在适配器中获取setUserData方法,该方法处理第一个活动与第二个活动之间的传递数据。我希望您在第一个活动中完美地从您的firebase数据库中获取数据。

onClick

然后在public void setUserData(String name, String image) { blogUserImage = mView.findViewById(R.id.blog_user_image); blogUserName = mView.findViewById(R.id.blog_user_name); blogUserName.setText(name); RequestOptions placeholderOption = new RequestOptions(); placeholderOption.placeholder(R.drawable.profile_placeholder); if (!((Activity) context).isFinishing()) { Glide.with(context).applyDefaultRequestOptions(placeholderOption).load(image).into(blogUserImage); } mView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(context, MainActivity.class); intent.putExtra("user_name", name); intent.putExtra("user_image", image); context.startActivity(intent); } }); } 中,只需从您的意图中获取额外内容并将其显示在所需位置。

MainActivity