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.
- 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.