1

ユーザーが不要になったファイルを削除できるようにするために、SD カードにあるディレクトリ内のファイルのリストを表示するリストビューがあります。ここで、各ファイルのパスを取得し、それをリストビュー内のそれぞれの項目にリンクして、ユーザーがファイルを削除できるようにする方法を見つける必要があります。項目を長押しするとポップアップするダイアログ ボックスを作成しました。人が「OK」ボタンを押した後、SDカード上のファイルを削除する必要があります。ファイルはすべて別のアクティビティで作成されるため、インテントを介して何かを渡す必要があると想定しています。

ReadFilesFromDirectory.java

public class ReadFilesFromPath extends Activity {
/** Called when the activity is first created. */
List<String> myList;
File file;
ListView listview;
ArrayAdapter<String> adapter;
String value;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recordinglist);
    Intent intent = getIntent();
    value = intent.getStringExtra("path"); //if it's a string you stored.
    listview = (ListView) findViewById(R.id.recordlist);
    myList = new ArrayList<String>();
    onitemclick();
    File directory = Environment.getExternalStorageDirectory();
    file = new File( directory + "/" + "Recordings" );
    File list[] = file.listFiles();

    for( int i=0; i< list.length; i++)
    {
            myList.add( list[i].getName() );
    }
    adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, myList);
    listview.setAdapter(adapter); //Set all the file in the list.
    longclick();
}

    public void longclick() {
        listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> arg0, View v,
                    int pos, long arg3) {
                AlertDialog.Builder adb=new AlertDialog.Builder(ReadFilesFromPath.this); //alert for each time an item is pressed
                  adb.setTitle("Delete?");
                  adb.setMessage("Are you sure you want to delete this recording?");
                  final int positionToRemove = pos;
                  adb.setNegativeButton("Cancel", null);
                  adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {

                      public void onClick(DialogInterface dialog, int which) {
                          listview.animate().setDuration(500).alpha(0) //animates a smooth deletion animation
                            .withEndAction(new Runnable() {
                              @Override
                              public void run() {

                           //DELETE THE FILE HERE

                                Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_SHORT).show();
                                myList.remove(positionToRemove); //removes the selected item
                                adapter.notifyDataSetChanged(); //tells the adapter to delete it
                                listview.setAlpha(1); 
                              }
                            }); 
                      }});
                  adb.show();
                  return true;
                  }
              });

            } 

public void onitemclick() {

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

              @Override
              public void onItemClick(AdapterView<?> parent, final View view,
                  int position, long id) {


    }
        }); 

}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
}



}

次のような別のアクティビティでファイルのデータを保存します (クラスには他にもメソッドがありますが、ファイルの保存には影響しません)。

public class MainActivity extends Activity {

MediaRecorder recorder;
File audiofile = null, file, directory;
private static final String TAG = "SoundRecordingActivity";
Button startButton, stopButton, openfiles, recordingbtn, delete, aboutbtn, exitbtn;
TextView welcometext;
SimpleDateFormat df;
String formattedDate, name, value, pathToExternalStorage;
String[] toastMessages;
File appDirectory, filetodelete;
int randomMsgIndex, number;
CheckBox mp4;
ContentValues values;
Uri base, newUri;
Boolean recordingstarted;
List<String> myList;
ListView listview;
ArrayAdapter<String> adapter;
private MenuDrawer mDrawer;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);
    mDrawer = MenuDrawer.attach(this, Position.BOTTOM);
    mDrawer.setContentView(R.layout.activity_main);
    mDrawer.setMenuView(R.layout.leftmenu);
    Calendar c = Calendar.getInstance();
    df = new SimpleDateFormat("hh:mm:ss");
    formattedDate = df.format(c.getTime());
    name = "Recording";
    listview = (ListView)findViewById(R.id.recordlist);
    exitbtn = (Button)findViewById(R.id.exitbtn);
    openfiles = (Button)findViewById(R.id.openfilesbtn);
    aboutbtn = (Button)findViewById(R.id.aboutbtn);
    startButton = (Button) findViewById(R.id.start);

    stopButton = (Button) findViewById(R.id.stop);
    pathToExternalStorage = Environment.getExternalStorageDirectory().toString();
    appDirectory = new File(pathToExternalStorage + "/" + "Recordify");

    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/robothin.ttf");
    welcometext = (TextView)findViewById(R.id.welcomeandtimetext);
    welcometext.setTypeface(myTypeface);
    onclicks();
    startandstop();

} 


protected void addRecordingToMediaLibrary() {
        values = new ContentValues(4);
        values.put(MediaStore.Audio.Media.TITLE, name); //"audio" + audiofile.getName()
        values.put(MediaStore.Audio.Media.MIME_TYPE, "video/mp4"); //sets the type to be a mp4 video file, despite being audio
        values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
        ContentResolver contentResolver = getContentResolver();
        base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        newUri = contentResolver.insert(base, values);
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
        welcometext.setText("Saved as " + audiofile); //teling users what name of file is called
    }
4

2 に答える 2

0

こうすれば

グローバルFile list[];にする

listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> arg0, View v,
                    final int pos, long arg3) {
                AlertDialog.Builder adb=new AlertDialog.Builder(ReadFilesFromPath.this); //alert for each time an item is pressed
                  adb.setTitle("Delete?");
                  adb.setMessage("Are you sure you want to delete this recording?");
                  final int positionToRemove = pos;
                  adb.setNegativeButton("Cancel", null);
                  adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {

                      public void onClick(DialogInterface dialog, int which) {
                          listview.animate().setDuration(500).alpha(0) //animates a smooth deletion animation
                            .withEndAction(new Runnable() {
                              @Override
                              public void run() {

                           //DELETE THE FILE HERE

                                try{
                                  list[pos].delete();
                                }catch(Exception e){
                                }



                                Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_SHORT).show();
                                myList.remove(positionToRemove); //removes the selected item
                                adapter.notifyDataSetChanged(); //tells the adapter to delete it
                                listview.setAlpha(1); 
                              }
                            }); 
                      }});
                  adb.show();
                  return true;
                  }
              });
于 2013-08-31T07:08:29.970 に答える