音频文件在存储中被删除但仍显示在自定义光标适配器的列表视图中

时间:2017-03-19 07:43:25

标签: android listview android-alertdialog android-cursoradapter

我正在创建一个音频列表,其中包含一个选项菜单按钮,显示两个操作重命名文件和删除文件,我为这两个操作使用了两个单独的警报对话框。问题是当我重命名音频文件时,它不会被删除。此外,我只能重命名该文件,以便在第二次尝试时单次尝试对话框未显示。我在存储中检查了文件被成功删除但文件仍然显示在列表视图中。

**光标适配器**

public class RecordsCursorAdapter extends CursorAdapter{

Context context;
android.support.v4.app.FragmentManager fragmentManager;
public RecordsCursorAdapter(Context context, Cursor c, android.support.v4.app.FragmentManager fragmentManager){
    super(context,c,0);
    this.fragmentManager=fragmentManager;
    this.context=context;
}
String AudioPath= Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator + "voices/" ;
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.single_list_item,parent,false);
}

@Override
public int getItemViewType(int position) {
    return super.getItemViewType(position);
}

@Override
public void bindView(View view, final Context context, Cursor mCursor) {
    TextView name= (TextView) view.findViewById(R.id.audio_name);
    TextView details= (TextView) view.findViewById(R.id.audio_details);
    ImageButton option= (ImageButton) view.findViewById(R.id.option);
    final ImageButton play=(ImageButton) view.findViewById(R.id.list_play);
    final String displayName = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
    final long durationMs=mCursor.getLong(mCursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
    Date date = new Date(durationMs);
    DateFormat formatter = new SimpleDateFormat("mm:ss");
    String dateFormatted = formatter.format(date);
    String dateTime=mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.DATE_ADDED));
    long size=mCursor.getLong(mCursor.getColumnIndex(MediaStore.Audio.Media.SIZE));
    String sizeKb=String.valueOf(size/1024);
    name.setText(displayName);
    Log.e("data:",dateFormatted+dateTime+sizeKb);
    details.setText(dateFormatted+" on "+dateTime+" "+sizeKb+"kb");

    //start playing an audio related to item clicked on list of audios with in dialog fragment (PlayerDialog)
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String currentAudioPath=AudioPath+ displayName ;
            PlayerDialog dialog=PlayerDialog.newInstance(displayName,durationMs,currentAudioPath);
            dialog.show(fragmentManager,"Path");
        }
    });
    //start playing an audio on play button of list clicked with in dialog fragment (PlayerDialog)
    play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String currentAudioPath=AudioPath+ displayName ;
            PlayerDialog dialog=PlayerDialog.newInstance(displayName,durationMs,currentAudioPath);
            dialog.show(fragmentManager,"Path");
        }
    });

    //seleting delete or edit audio name by option icon on listview clicked

    option.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PopupMenu popupMenu=new PopupMenu(context,v);
            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId())
                    {
                        case R.id.action_remove_list:
                            deleteDialog(displayName);
                            break;
                        case R.id.action_edit:
                            String subName=displayName.substring(0,19);
                            renameDialog(subName);
                            break;
                    }
                    return false;
                }
            });
            popupMenu.inflate(R.menu.option_menu);
            popupMenu.show();

        }
    });

}


//dialog show from option menu Rename item clicked for renaming the audio name
public void renameDialog(final String currentName)
{
    Log.e("currentName:",currentName);
    AlertDialog.Builder alertDialog=new AlertDialog.Builder(context);
    alertDialog.setMessage("Rename Audio");
    final EditText input=new EditText(context);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT);
    input.setLayoutParams(lp);
    alertDialog.setView(input);
    input.setText(currentName);
    alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            String pathfile=Environment.getExternalStorageDirectory()+ File.separator + "voices/";
            ContentValues values=new ContentValues();
            File sdCard=new File(pathfile);
            final File from=new File(sdCard,currentName+".mp3");
            final File to=new File(sdCard,input.getText().toString()+".mp3");
            from.renameTo(to);
            values.put(MediaStore.Audio.Media.DISPLAY_NAME,input.getText().toString()+".mp3");
            context.getContentResolver().update(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,values,MediaStore.Audio.Media.DATA+ "=?",new String[]{pathfile+currentName+".mp3"});
        }
    });
    alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    alertDialog.show();
}

//dialog show to remove the audio file selected from the list through option menu icon
public void deleteDialog(final String currentAudio){
    AlertDialog.Builder alertDialog=new AlertDialog.Builder(context);
  alertDialog.setTitle("Delete");
    alertDialog.setMessage(currentAudio);
    alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            String pathfile=Environment.getExternalStorageDirectory()+ File.separator + "voices/";
            context.getContentResolver().delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,MediaStore.Audio.Media.DATA+ "=?",new String[]{pathfile+currentAudio});
                 File f=new File(pathfile+currentAudio);
                 f.delete();
          }
    });
    alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
        }
    });
    alertDialog.show();
}

屏幕截图

enter image description here

0 个答案:

没有答案