-2

I am creating a video player which has a rename video feature and almost I have done the renaming feature work but it's not working correctly.

  1. renaming the file but not updating the video list and recycler view.

Using this adapter click it's updating the name in the video and recyclerview also but not updating on the correct position even though I am using the position variable but still it's not updating correctly.

Example: If I want to update the 3rd video from the list it's updating the 2nd video and If I want to update the last video and updates correctly.

adapter.setOnItemClickListner(new video_adepter.onItemClickListner() {
            @Override
            public void onClick(int position, String new_name) {
                Toast.makeText(getContext(), "update text " + video_list.get(position).getTxt_video_name(), Toast.LENGTH_SHORT).show();
                video_list.get(position).setTxt_video_name(new_name);
                adapter.notifyDataSetChanged();


            }
        });

I have posted all the codes below.

video_adapter.java

case R.id.action_add_rename:

                    f_file = new File(Path);
                    final File from = new File(Path);

                    Fragment currentFragment = ((AppCompatActivity) mContext).getSupportFragmentManager().findFragmentById(R.id.fragment_container);
                    if (currentFragment instanceof folder_video_list) {
                        folder_video_list = currentFragment;

                    }

                    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
                    alertDialog.setTitle("Rename To");
                    final EditText editTextDialog = new EditText(mContext);

                    String mediaName = f_file.getName();
                    mediaName = mediaName.substring(0, mediaName.lastIndexOf("."));
                    editTextDialog.setText(mediaName);
                    alertDialog.setView(editTextDialog);
                    editTextDialog.requestFocus();
                    alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //Apply validation check
                            if(TextUtils.isEmpty(editTextDialog.getText().toString()))
                            {
                                Toast.makeText(mContext,"Can't rename empty file",Toast.LENGTH_SHORT).show();
                                return;
                            }
                            String onlyPath = f_file.getParentFile().getAbsolutePath();
                            Toast.makeText(mContext, "Original Path: " + onlyPath, Toast.LENGTH_SHORT).show();
                            String ext = f_file.getAbsolutePath();
                            Toast.makeText(mContext, "ext: file.getAbsolutePath(): " + ext, Toast.LENGTH_SHORT).show();
                            ext = ext.substring(ext.lastIndexOf("."));

                            String newPath = onlyPath + "/" + editTextDialog.getText().toString().trim() + ext;
                            Toast.makeText(mContext, "New Path: " + newPath, Toast.LENGTH_SHORT).show();

                            File newFile = new File(newPath);
                            Toast.makeText(mContext, "renaming: in onClick(): newFile name: "+newFile.getName(), Toast.LENGTH_SHORT).show();
                            boolean rename=false;
                            try {
                                rename = f_file.renameTo(newFile);
                                Toast.makeText(mContext, "file name after renaming: "+f_file.getName(), Toast.LENGTH_SHORT).show();
                            } catch (Exception e) {
                                Toast.makeText(mContext, "Exception while renaming: " + e, Toast.LENGTH_SHORT).show();
                            }
                            Toast.makeText(mContext, "rename: " + rename, Toast.LENGTH_SHORT).show();
                            if (rename) {
                                ContentResolver resolver = mContext.getApplicationContext().getContentResolver();
                                resolver.delete(MediaStore.Files.getContentUri("external"),
                                        MediaStore.MediaColumns.DATA + "=?", new String[]
                                                {
                                                        f_file.getAbsolutePath()
                                                });
                                Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                                intent.setData(Uri.fromFile(newFile));
                                mContext.getApplicationContext().sendBroadcast(intent);
                                Toast.makeText(mContext, "File Renamed", Toast.LENGTH_SHORT).show();
                                onItemClickListner.onClick(position,editTextDialog.getText().toString());

                            } else {
                                Toast.makeText(mContext, "Process Failed!", Toast.LENGTH_SHORT).show();
                            }
                            notifyDataSetChanged();
                        }
                    });
                    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //dismiss the dialog
                            dialogInterface.dismiss();
                        }
                    });
                    alertDialog.create().show();

folder_video_list.java (Fragment)

RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 1);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListner(new video_adepter.onItemClickListner() {
            @Override
            public void onClick(int position, String new_name) {
                Toast.makeText(getContext(), "update text " + video_list.get(position).getTxt_video_name(), Toast.LENGTH_SHORT).show();
                video_list.get(position).setTxt_video_name(new_name);
                adapter.notifyDataSetChanged();


            }
        });

Please help me to rename a video and update its name instantly on recyclerview.

Thank you.

  • You shouldn't just paste all your code in the question. Can you tell us what you do and where the important parts are. What is with the recycler view. Where is the element layot defined. I don't think that anyone is able to give a solution based on the code you posted – TIMBLOCKER Nov 11 '22 at 07:53
  • All codes mean full codes of the fragment and adapter? – Equalto multiply Nov 11 '22 at 11:10
  • Yes. Also all important other code snippets and either you ccmment them in your code or you comment them in the question. – TIMBLOCKER Nov 11 '22 at 15:30
  • Basically, I want to rename a video from the recyclerview Using DialogBoxAlert and I have pasted all the codes above that codes are working fine but the recyclerview not renaming it instantly I need to go back and reopen the app to see the changes. – Equalto multiply Nov 12 '22 at 08:36

1 Answers1

0

So your problem seems to be that the data you insert into your RecyclerView is not updated during runtime. For that you should look in to this answer:

https://stackoverflow.com/a/48959184/15959434

The gist is, that you basically have to tell the recycler view, that the data has changed so that the Views can be reinstantiated. Either you just tell it to update or insert one element or you just make it reinstantiate all elements.

These methods should be executed in your alertDialog.setPositiveButton. If you then update the RecyclerView your name should be updated as well.

TIMBLOCKER
  • 328
  • 4
  • 15
  • No these codes can't work. I just want to rename my file which is listed in the recyclerview after clicking on `PositiveButton` from dialogBox it's updated in the File Manager but not updating on my screenView (recyclerView). – Equalto multiply Nov 13 '22 at 14:08
  • What are you doing there? Can you provide Screenshots? The problem is that you are perfoming the update of the name but not reinstantiating the recyclerview. Have you tried deleting ´notifyDataSetChanged´ and replacing it with ´adapter.notifyItemInserted(insertIndex);´... – TIMBLOCKER Nov 13 '22 at 14:24