我的应用程序从内部存储中的文件夹中获取mp3文件,我有一个问题是添加删除方法,用于从循环视图和内部手机存储中删除mp3项目,我想订购从最新到较旧的项目。 感谢你们对我的帮助 这是我的代码:
录音班:
public class Recording {
String Uri, fileName;
boolean isPlaying = false;
public Recording(String uri, String fileName, boolean isPlaying) {
Uri = uri;
this.fileName = fileName;
this.isPlaying = isPlaying;
}
public String getUri() {
return Uri;
}
public String getFileName() {
return fileName;
}
public boolean isPlaying() {
return isPlaying;
}
public void setPlaying(boolean playing){
this.isPlaying = playing;
}
}
这个适配器:
package com.recorderlite.audio.audiorecorderlite;
import android.content.Context;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.transition.TransitionManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
/**
* Created by Oualid on 29/10/2017.
*/
public class RecordingAdapter extends
RecyclerView.Adapter<RecordingAdapter.ViewHolder> {
private Context context;
private ArrayList<Recording> recordingArrayList;
private MediaPlayer mPlayer;
private boolean isPlaying = false;
private int last_index = -1;
private int adapterPosition;
public Recording recording ;
public RecordingAdapter(Context context, ArrayList<Recording> recordingArrayList){
this.context = context;
this.recordingArrayList = recordingArrayList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.recording_item_layout, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
setUpData(holder,position);
}
private void setUpData(ViewHolder holder, int position) {
Recording recording = recordingArrayList.get(position);
holder.textViewName.setText(recording.getFileName());
if( recording.isPlaying() ){
holder.imageViewPlay.setImageResource(R.drawable.ic_pause);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
TransitionManager.beginDelayedTransition((ViewGroup) holder.itemView);
}
holder.seekBar.setVisibility(View.VISIBLE);
holder.seekUpdation(holder);
}else{
holder.imageViewPlay.setImageResource(R.drawable.ic_play);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
TransitionManager.beginDelayedTransition((ViewGroup) holder.itemView);
}
holder.seekBar.setVisibility(View.GONE);
}
holder.manageSeekBar(holder);
}
@Override
public int getItemCount() {
return recordingArrayList.size();
}
public void onClick(View view) {
removeAt(getAdapterPosition());
}
public void removeAt(int position) {
recordingArrayList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, recordingArrayList.size());
String uri = recording.getUri();
File file = new File (uri);
file.delete();
}
public int getAdapterPosition() {
return adapterPosition;
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageViewPlay;
SeekBar seekBar;
TextView textViewName;
private String recordingUri;
private int lastProgress = 0;
private Handler mHandler = new Handler();
ViewHolder holder;
public ViewHolder(View itemView) {
super(itemView);
Button btn_delete ;
imageViewPlay = itemView.findViewById(R.id.imageViewPlay);
seekBar = itemView.findViewById(R.id.seekBar);
textViewName = itemView.findViewById(R.id.textViewRecordingname);
btn_delete = itemView.findViewById(R.id.button2);
btn_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
removeAt(getAdapterPosition());
}
});
imageViewPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = getAdapterPosition();
Recording recording = recordingArrayList.get(position);
recordingUri = recording.getUri();
if( isPlaying ){
stopPlaying();
if( position == last_index ){
recording.setPlaying(false);
stopPlaying();
notifyItemChanged(position);
}else{
markAllPaused();
recording.setPlaying(true);
notifyItemChanged(position);
startPlaying(recording,position);
last_index = position;
}
}else {
if( recording.isPlaying() ){
recording.setPlaying(false);
stopPlaying();
Log.d("isPlayin","True");
}else {
startPlaying(recording,position);
recording.setPlaying(true);
seekBar.setMax(mPlayer.getDuration());
Log.d("isPlayin","False");
}
notifyItemChanged(position);
last_index = position;
}
}
});
}
public void manageSeekBar(ViewHolder holder){
holder.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if( mPlayer!=null && fromUser ){
mPlayer.seekTo(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
private void markAllPaused() {
for( int i=0; i < recordingArrayList.size(); i++ ){
recordingArrayList.get(i).setPlaying(false);
recordingArrayList.set(i,recordingArrayList.get(i));
}
notifyDataSetChanged();
}
Runnable runnable = new Runnable() {
@Override
public void run() {
seekUpdation(holder);
}
};
private void seekUpdation(ViewHolder holder) {
this.holder = holder;
if(mPlayer != null){
int mCurrentPosition = mPlayer.getCurrentPosition() ;
holder.seekBar.setMax(mPlayer.getDuration());
holder.seekBar.setProgress(mCurrentPosition);
lastProgress = mCurrentPosition;
}
mHandler.postDelayed(runnable, 100);
}
private void stopPlaying() {
try{
mPlayer.release();
}catch (Exception e){
e.printStackTrace();
}
mPlayer = null;
isPlaying = false;
}
private void startPlaying(final Recording audio, final int position) {
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(recordingUri);
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
Log.e("LOG_TAG", "prepare() failed");
}
//showing the pause button
seekBar.setMax(mPlayer.getDuration());
isPlaying = true;
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
audio.setPlaying(false);
notifyItemChanged(position);
}
});
}
}
//this method will remove the item from the list
private void removeHero(final int position) {
//Creating an alert dialog to confirm the deletion
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Are you sure you want to delete this?");
//if the response is positive in the alert
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//removing the item
recordingArrayList.remove(position);
//reloading the list
notifyDataSetChanged();
}
});
//if response is negative nothing is being done
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
//creating and displaying the alert dialog
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
这是录制列表活动:
package com.recorderlite.audio.audiorecorderlite;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import java.io.File;
import java.util.ArrayList;
public class RecordingListActivity extends AppCompatActivity {
private Toolbar toolbar;
private RecyclerView recyclerViewRecordings;
private ArrayList<Recording> recordingArraylist;
private RecordingAdapter recordingAdapter;
private TextView textViewNoRecordings;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recording_list);
recordingArraylist = new ArrayList<Recording>();
initViews();
fetchRecordings();
}
private void fetchRecordings() {
File root = android.os.Environment.getExternalStorageDirectory();
String path = root.getAbsolutePath() + "/AudioRecorderLite/Audios";
Log.d("Files", "Path: " + path);
File directory = new File(path);
File[] files = directory.listFiles();
Log.d("Files", "Size: "+ files.length);
if( files!=null ){
for (int i = 0; i < files.length; i++) {
Log.d("Files", "FileName:" + files[i].getName());
String fileName = files[i].getName();
String recordingUri = root.getAbsolutePath() + "/AudioRecorderLite/Audios/" + fileName;
Recording recording = new Recording(recordingUri,fileName,false);
recordingArraylist.add(recording);
}
textViewNoRecordings.setVisibility(View.GONE);
recyclerViewRecordings.setVisibility(View.VISIBLE);
setAdaptertoRecyclerView();
}else{
textViewNoRecordings.setVisibility(View.VISIBLE);
recyclerViewRecordings.setVisibility(View.GONE);
}
}
private void setAdaptertoRecyclerView() {
recordingAdapter = new RecordingAdapter(this,recordingArraylist);
recyclerViewRecordings.setAdapter(recordingAdapter);
}
private void initViews() {
/** setting up the toolbar **/
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(R.string.recording_list);
toolbar.setTitleTextColor(getResources().getColor(android.R.color.holo_orange_dark));
setSupportActionBar(toolbar);
/** enabling back button ***/
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
/** setting up recyclerView **/
recyclerViewRecordings = (RecyclerView) findViewById(R.id.recyclerViewRecordings);
recyclerViewRecordings.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL, false));
recyclerViewRecordings.setHasFixedSize(true);
textViewNoRecordings = (TextView) findViewById(R.id.textViewNoRecordings);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void onClick(View view) {
}
}
答案 0 :(得分:0)
尝试更改以下代码
public void removeAt(int position) {
recordingArrayList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, recordingArrayList.size());
String uri = recordingArrayList.get(position).getUri();
File file = new File (uri);
file.delete();
}
String uri = recording.getUri();
当您从空对象获取uri时,此行会导致问题,因此请将其更改为
String uri = recordingArrayList.get(position).getUri();
答案 1 :(得分:0)
我已经做到了,而且效果很好。
您必须需要要从创建的目录中删除的文件名。要调用该函数,请使用deleteItem(getAdaptorPosition());
private void deleteItem(int index) {
recordingArrayList.remove(index);
notifyItemRemoved(index);
notifyItemRangeChanged(index, recordingArrayList.size());
File root = android.os.Environment.getExternalStorageDirectory();
String path = root.getAbsolutePath() + "/AudioRecorderLite/Audios/";
Log.d("Files", "Path: " + path);
File directory = new File(path);
File[] files = directory.listFiles();
Log.d("Files", "Size: "+ files.length);
String fileName = files[index].getName();
String recordingUri = root.getAbsolutePath() + "/AudioRecorderLite/Audios/" + fileName;
File myfile = new File(recordingUri);
myfile.delete();
}