Reputation: 82
I have a Recylerview
with Button
and TextView
as params. I am opening a file chooser on button click.
@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);
}
});
}
Now, after selecting the file I am getting the file name in the OnActivityResult
displayName variable. I want to set the 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);
}
Help me out , how to place the ViewHolder
params to outside of the Adapter
.
Upvotes: 1
Views: 273
Reputation: 2143
Few notes
The ViewHolder
's holder
params is supposed to managed only by the Adapter
class itself.
What you can do
Hold a reference for the currently selected file/s received from the onActivityResult
, either by using local store like (List
, SharedPreferences
, Realm
, etc.)
Populate the file chooser adapter again with the latest file items from the list
Invoke 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.
Read more on
Upvotes: 2
Reputation: 5705
You should not handle click events in the adapter itself. Instead forward them back to activity/fragment using interfaces.
So when you click on the button, the method of the interface is called in the activity/fragment and from their you can easily detect the onActivityResult()
method.
So when you get the name, update that value in your adapter dataset and notify your adapter for the changes.
A simple interface like this(Code is in Kotlin)
interface OnClickListener {
fun onClick(position:Int)
}
And make your activity implement it. Now in your adapter class constructor pass the interface as a parameter.
AdapterClass(private val listener: OnClickListener)
Upvotes: 0
Reputation: 3285
You will need to set the file name in your DataSet.
And call notifyDataSetChange()
on Activity Result.
Upvotes: 1