2017-08-13 157 views
1

我想從列表視圖中刪除文件,我能夠刪除原始文件但不刪除列表中的名稱,我認爲它是因爲我需要刪除它從數據庫中,問題是我不知道如何,有人可以幫忙嗎?刪除文件並從列表中刪除它的名字

//適配器

 listAdapter = new ArrayAdapter<String>(RecordsActivity.this, R.layout.support_simple_spinner_dropdown_item, MainActivity.listRecord); 
    recordList.setAdapter(listAdapter); 

//刪除文件(正常,名字是不是從我猜數據庫中刪除)

 delete.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      try { 
       datadelete(filePath); 
       listAdapter.remove(recordToPlay); 
       listAdapter.notifyDataSetChanged(); 
       delete.setVisibility(View.INVISIBLE); 
       share.setVisibility(View.INVISIBLE); 
       Toast.makeText(getApplicationContext(), "File deleted successfully", Toast.LENGTH_SHORT).show(); 


      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } 

     } 
    }); 

//該dataDelete方法

public void dataDelete(String inputPath) throws FileNotFoundException { 
    try { 
     // delete the original file 
     new File(inputPath).delete(); 

    } catch (Exception e) { 
     Log.e("tag", e.getMessage()); 
    } 
} 

//主要活動

  File directory = new File(Environment.getExternalStorageDirectory() + File.separator + "/Recordings"); 
     directory.mkdirs(); 

     //outPutFile = Environment.getExternalStorageDirectory().toString() + "/recording.3gp"; 
     String dateTime = new SimpleDateFormat("dd.MM.yyyy hh-mm-ss aa", Locale.getDefault()).format(new Date()); 
     //Date date = new Date(); 
     //String dateTime = DateFormat.getDateTimeInstance().format(date); 
     outPutFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Recordings/"+dateTime +".m4a"; 

     final String DIR_DATABASE = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Recordings"; 
     String sqliteQuery = "CREATE TABLE IF NOT EXISTS Recordings (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , fileName VARCHAR)"; 
     database = SQLiteDatabase.openOrCreateDatabase(DIR_DATABASE + "db.sqlite", null); 
     database.execSQL(sqliteQuery); 

     getNames(); 

//用於檢索文件的名稱

public static void getNames(){ 
    Cursor cursor = database.rawQuery("SELECT fileName FROM Recordings", null); 
    ArrayList<String> fileNames = new ArrayList<>(); 
    while (cursor.moveToNext()) { 

     String fileName = cursor.getString(0); 
     fileNames.add(fileName); 
    } 
    cursor.close(); 
    database.close(); 

    listRecord.addAll(fileNames); 
} 

的getNames方法//這是我保存文件

  stop.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       mediaRecorder.stop(); 
       mediaRecorder.release(); 
       mediaRecorder = null; 
       stop.setEnabled(false); 
       pauseBtn.setEnabled(false); 

       SQLiteDatabase database = SQLiteDatabase.openDatabase(DIR_DATABASE + "db.sqlite", null, 0); 
       values = new ContentValues(); 
       values.put("fileName", outPutFile.substring(31)); 
       database.insert("Recordings", "", values); 


       Toast.makeText(getApplicationContext(), "Audio recorded", Toast.LENGTH_LONG).show(); 

       Activity mActivity = MainActivity.this; 
       restartActivity(mActivity); 


      } 
     }); 

回答

1

您應該添加刪除查詢刪除數據來自數據庫。將您的代碼更改爲

delete.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     try { 
      datadelete(filePath); 
      database = this.getWritableDatabase(); 
      database.execSQL("DELETE FROM Recordings WHERE fileName = '"+your file name Here+"'"); 
      database.close(); 
      listAdapter.remove(recordToPlay); 
      listAdapter.notifyDataSetChanged(); 

      delete.setVisibility(View.INVISIBLE); 
      share.setVisibility(View.INVISIBLE); 
      Toast.makeText(getApplicationContext(), "File deleted successfully", Toast.LENGTH_SHORT).show(); 


     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

    } 
}); 

您應該在sqlite查詢中編寫要刪除的文件名。希望這會起作用。