android - onActivityResult中访问ViewHolder参数
问题描述
我有一个Recylerview
withButton
和TextView
as 参数。我在单击按钮时打开文件选择器。
@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变量中获取文件名。我想设置holder.textview.setText(displayName);
in onActivityResult
:
@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
.
解决方案
几条笔记
的参数应该只由类本身管理ViewHolder
。holder
Adapter
你可以做什么
保存从 接收到的当前选定文件的引用
onActivityResult
,或者使用本地存储,如 (List
,SharedPreferences
,Realm
等)使用列表中的最新文件项再次填充文件选择器适配器
调用
notifyDataSetChanged()
公共无效 notifyDataSetChanged ()
通知附加的观察者底层数据已更改,任何反映数据集的视图都应自行刷新。
阅读更多
推荐阅读
- javascript - 在这个 jQuery 验证案例中,如何避免重复 isValid?
- typescript - 如何在打字稿中分配通用条件推断类型的值?
- python - 用 Selenium python 中的 bb 代码填充 textarea
- python - 如何从pyqt5中的父MainWindow类继承自我
- angular - HeaderComponent.logout2 处未定义的属性“注销”
- javascript - 在 Angular 6 应用程序中通过 javascript 更改 base64 图像大小并使用不同的扩展名(png、jpg、gif 等)下载
- javascript - 如何映射对象属性
- mysql - 根据当前表和每个单独的行向 SQL SELECT 查询输出添加额外的列
- ffmpeg - 如何使用 FFMPEG 提取无损图像序列并将它们传送到 mozjpeg 的 cjpeg?
- java - 在 android 4.4 中注意到浮动操作按钮周围的奇怪圆圈?