1

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.

remudada
  • 3,751
  • 2
  • 33
  • 60
Meena Dev
  • 82
  • 1
  • 9
  • You need to add this name to your dataset of adapter and then call `#notifyItemChange()` on Adapter . Make `super.onActivityResult` as first line of the method . – ADM Jan 22 '19 at 10:07
  • use interfaces and when you gets the result `adapterobject.notifyItemChanged(position)` – Ashvin solanki Jan 22 '19 at 10:14

3 Answers3

2

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

Community
  • 1
  • 1
Joshua de Guzman
  • 2,063
  • 9
  • 24
1

You will need to set the file name in your DataSet. And call notifyDataSetChange() on Activity Result.

Milind Mevada
  • 3,145
  • 1
  • 14
  • 22
  • pojo.setFileName(displayName); pojoClassList.set(tempPosition, pojo); Iam setting this under OnActivityResult,but same file name get updates for the previous lists.How to set the filename using position. – Meena Dev Jan 22 '19 at 11:13
  • You will need to hold the position of a clicked item and update the list on same item. – Milind Mevada Jan 22 '19 at 11:15
0

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)
Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84