android - 在 onActivityResult 中访问 ViewHolder 参数

标签 android android-recyclerview onactivityresult recyclerview-layout

我有一个 RecylerviewButtonTextView 作为参数。我在点击按钮时打开一个文件选择器

@Override
public void onBindViewHolder(final FileChooserAdapter.MyViewHolder holder, final int position) {
    PojoClass pojoClass = pojoClassList_.get(position);
    holder.listViewName.setText(pojoClass.getListName());
    holder.fileBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent filePickIntent = new Intent(Intent.ACTION_GET_CONTENT);
            filePickIntent.setType("*/*");
            startActivityForResult(filePickIntent, 1);
        }
    });

}

现在,选择文件后,我在 OnActivityResult displayName 变量中获取文件名。我想在 onActivityResult 中设置 holder.textview.setText(displayName);:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case 1:
        // Get the Uri of the selected file
        Uri uri = data.getData();
        String uriString = uri.toString();
        File myFile = new File(uriString);
        String path = myFile.getAbsolutePath();


        if (uriString.startsWith("content://")) {
            Cursor cursor = null;
            try {
                cursor = getContentResolver().query(uri, null, null, null, null);
                if (cursor != null && cursor.moveToFirst()) {
                    displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                }
            } finally {
                cursor.close();
            }
        } else if (uriString.startsWith("file://")) {
            displayName = myFile.getName();
        }
        // I want to place the holder.textview.setText(displayName) here
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

帮助我,如何将 ViewHolder 参数放置到 Adapter 之外。

最佳答案

一些笔记

ViewHolderholder 参数应该仅由Adapter 类本身管理。

你能做什么

  • 通过使用像 (List, SharedPreferences领域等)

  • 使用列表中的最新文件项再次填充文件选择器适配器

  • 调用notifyDataSetChanged()

public void notifyDataSetChanged ()

Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

阅读更多

关于android - 在 onActivityResult 中访问 ViewHolder 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54305717/

相关文章:

android - 如何在 ScrollView 中滚动 RecyclerView

java - 支持 v.23.2.0 的 RecyclerView 消失动画中的 NullPointerException

Android:如何让一个 Activity 将结果返回给调用它的 Activity ?

android - Viewpager android 中的水平 Recyclerview

java - 如何在单个 Activity 中添加多个 onActivityResult() 而不转到其他 Activity?

Android Activity 生命周期

java - ArrayList 或 HashMap<String, String> 的不同排序方式

java - 在 Android 应用程序中使用 http 客户端模拟表单发布?

android - - ddms] 传输错误 : open failed:Permission denied

Android sherlock 库无效选项菜单问题