RecyclerView.Adapter内部的onActivityResult未使用 [英] onActivityResult inside a RecyclerView.Adapter not being used

查看:302
本文介绍了RecyclerView.Adapter内部的onActivityResult未使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在适配器中有一个进入图库的适配器:

    MyAdapter extends
            RecyclerView.Adapter<RecyclerView.ViewHolder> {
    ...
    onClic..{
        Intent intent = new Intent(
                                    Intent.ACTION_PICK,
                                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                            intent.setType("image/*");
                            ((Activity) context).startActivityForResult(
                                    Intent.createChooser(intent, "Select File"),
                                    SELECT_FILE);}
    ....
 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
        }
    }
..
    }

我想做的是用从图库中选择的图像更新ImageView,但是为了做到这一点,我必须使用onActivityResult,并且编译器抱怨method onActivityResult is never usedcannot resolve method onActivityResult.

我该如何处理?

解决方案

请注意,您正在使用Activity调用startActivityForResult:

((Activity) context).startActivityForResult();

onActivityResult(...)是一种回调方法,应与用于调用startActivityForResult()的同一Activity.

由于没有这样的方法来覆盖名为onActivityResult(...)RecyclerView.Adapter,因此您会遇到编译器错误.

由于您询问如何正确执行此操作,因此这里是一个选择.

将以下界面添加到MyAdapter:

public interface OnClickImageListener{
    void onClick();
}

然后让您的对话框实现该接口.在onClick方法中,执行以下操作:

@Override
public void onClick() {
    Intent intent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    intent.setType("image/*");
    startActivityForResult(
           Intent.createChooser(intent, "Select File"), SELECT_FILE);
}

您可以将onActivityResult(...)方法添加到Fragment中,该方法现在将被调用.

要在创建MyAdapter时使用此功能,请将Fragment作为参数传递给构造函数,并将其作为OnClickImageListener引用,这样适配器中的单击侦听器将变成:

imageClickListener.onClick();

还请注意,您可以将索引添加到onClick()方法中,或者您需要知道适配器返回时将其填充到适配器中的哪个项目的任何其他信息.

I have a button inside an Adapter which goes into gallery:

    MyAdapter extends
            RecyclerView.Adapter<RecyclerView.ViewHolder> {
    ...
    onClic..{
        Intent intent = new Intent(
                                    Intent.ACTION_PICK,
                                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                            intent.setType("image/*");
                            ((Activity) context).startActivityForResult(
                                    Intent.createChooser(intent, "Select File"),
                                    SELECT_FILE);}
    ....
 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
        }
    }
..
    }

What I'm trying to do is to update an ImageView with the image selected from the gallery, but in order to do that I have to use onActivityResult and the compiler is complaining method onActivityResult is never used and cannot resolve method onActivityResult.

How can I go about this ?

解决方案

Note on this line you are using an Activity to call startActivityForResult:

((Activity) context).startActivityForResult();

onActivityResult(...) is a callback method and should be in the same Activity you used to call startActivityForResult().

You are getting the compiler error because there is no such method to override named onActivityResult(...) for RecyclerView.Adapter.

EDIT:

Since you asked how you can do this properly here is one option.

Add the following interface to MyAdapter:

public interface OnClickImageListener{
    void onClick();
}

Then have your dialog implement that interface. In the onClick method do:

@Override
public void onClick() {
    Intent intent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    intent.setType("image/*");
    startActivityForResult(
           Intent.createChooser(intent, "Select File"), SELECT_FILE);
}

You can add the onActivityResult(...) method to your Fragment and it will now be called.

To use this when you create MyAdapter pass the Fragment in as an argument to the constructor and reference it as a OnClickImageListener so your click listener in the adapter simply becomes:

imageClickListener.onClick();

Also note you can add an index to the onClick() method or whatever else you need to know which item in the adapter to populate the image with once it is returned.

这篇关于RecyclerView.Adapter内部的onActivityResult未使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆