You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Dialog Fragment的Uri值传递到Adapter的onBindViewHolder方法

解决Dialog Fragment图片URL传递到同Activity RecyclerView Adapter的问题

问题场景

需要将Dialog Fragment中uploadImageToFirebase(Uri imageUri)方法成功上传后获取到的图片下载URL(String image = uri.toString()),传递给同属一个Activity的AddStudentAdapter的onBindViewHolder方法,以此在RecyclerView的列表项中显示对应图片。

相关代码片段

Dialog Fragment中的上传方法:

private void uploadImageToFirebase(Uri imageUri) {
    AddUserStudent addUserStudentCourse = (AddUserStudent) getActivity();
    String importClass = addUserStudentCourse.exportCourse();

    AddUserStudent addUserStudentSection = (AddUserStudent) getActivity();
    String importSection = addUserStudentSection.exportSection();

    user = FirebaseAuth.getInstance().getCurrentUser();
    userId = user.getUid();
    StorageReference fileRef = storageReference.child("Students/" + FirebaseDatabase.getInstance().getReference("Users").child(userId).child("Courses").child(importClass).child("Section").child(importSection).child("Students").child(sFullname.getText().toString()) +"/profile.jpg");
    fileRef.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

            fileRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                    Picasso.get().load(uri).transform(new CropCircleTransformation()).into(studentImage);
                    String image = uri.toString();
                    Toast.makeText(getContext(), image, Toast.LENGTH_SHORT).show();

                }
            });
            Toast.makeText(getContext(), "Image Uploaded", Toast.LENGTH_SHORT).show();

        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(getContext(), "Uploading Image Failed", Toast.LENGTH_SHORT).show();
        }
    });
}

目标Adapter的onBindViewHolder方法:

public class AddStudentAdapter extends FirebaseRecyclerAdapter<AddStudent, AddStudentAdapter.myViewHodler> {

    AlertDialog.Builder builder;
    public AddStudentAdapter(@NonNull FirebaseRecyclerOptions<AddStudent> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull myViewHodler holder, int position, @NonNull AddStudent model) {
            holder.sName.setText(model.studentFullname);
            holder.sEmail.setText(model.studentEmail);
            holder.sAge.setText(model.studentAge);
//        Picasso.get()          // 这里需要传入Dialog Fragment中获取的图片URL
//                .load()
//                        .into(holder.imgStudent);

}
}

尝试过的失败方案

曾尝试用Bundle和Intent将Uri从Dialog Fragment传递到主Activity,再在Adapter中调用主Activity的存储方法,但出现空指针异常。

可行解决方案

通过在主Activity中编写用于存储和获取图片URL的方法,结合回调传递值,最终解决问题:

  1. 在主Activity中添加存储图片URL的变量及配套方法:
private String imageUri;

// 存储图片URL的方法
public String getUri(String uri){
    this.imageUri = uri;
    return uri;
}

// 给Adapter提供获取URL的方法
public String getImageUri() {
    return imageUri;
}
  1. 在Dialog Fragment的图片上传成功回调中,调用主Activity的getUri方法传入URL,并通知Adapter更新:
// 在fileRef.getDownloadUrl()的onSuccess回调里添加
String image = uri.toString();
AddUserStudent mainActivity = (AddUserStudent) getActivity();
if(mainActivity != null) {
    mainActivity.getUri(image);
    // 通知Adapter刷新视图
    mainActivity.getAddStudentAdapter().notifyDataSetChanged();
}
  1. 在Adapter的onBindViewHolder中获取URL并加载图片:
@Override
protected void onBindViewHolder(@NonNull myViewHodler holder, int position, @NonNull AddStudent model) {
    holder.sName.setText(model.studentFullname);
    holder.sEmail.setText(model.studentEmail);
    holder.sAge.setText(model.studentAge);
    
    // 从主Activity获取图片URL并加载
    AddUserStudent mainActivity = (AddUserStudent) holder.itemView.getContext();
    String imageUrl = mainActivity.getImageUri();
    if(imageUrl != null) {
        Picasso.get()
                .load(imageUrl)
                .transform(new CropCircleTransformation())
                .into(holder.imgStudent);
    }
}

这样就能将Dialog中获取的图片URL传递到Adapter,实现RecyclerView列表项的图片显示。

内容的提问来源于stack exchange,提问作者compsci

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 20:05:47