我有一个 Recylerview
以 Button
和 TextView
作为参数。我在点击按钮时打开一个文件选择器。
@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
之外。
最佳答案
一些笔记
ViewHolder
的holder
参数应该仅由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/