Android Studio 在android中重命名视频文件后刷新recyclerview

y1aodyip  于 2022-11-16  发布在  Android
关注(0)|答案(1)|浏览(193)

我正在创建一个视频播放器,它有一个重命名视频功能,几乎我已经做了重命名功能的工作,但它不能正常工作。
1.重命名文件,但不更新视频列表和回收程序视图。

使用此适配器,单击它更新视频和recyclerview中的名称,但没有在正确的位置更新,即使我使用位置变量,但它仍然没有正确更新。
示例:如果我想更新列表中的第三个视频,则会更新第二个视频;如果我想更新最后一个视频,则会正确更新。

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();

            }
        });

我已经张贴了下面的所有代码。

视频适配器.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();

文件夹_视频_列表.java(片段)

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();

            }
        });

请帮我重命名一个视频,并立即更新其名称上recyclerview。

  • 谢谢-谢谢
f45qwnt8

f45qwnt81#

所以你的问题似乎是你插入到RecyclerView中的数据在运行时没有更新。
https://stackoverflow.com/a/48959184/15959434
要点是,你基本上必须告诉回收器视图,数据已经改变,以便视图可以被重新示例化。要么你只是告诉它更新或插入一个元素,要么你只是让它重新示例化所有元素。
这些方法应该在您的alertDialog.setPositiveButton中执行。如果您随后更新RecyclerView,您的名称也应该更新。

相关问题