2015-02-06 84 views
1

我是Java新手,一起編碼,所以我用我的代碼打了一堵牆。我幾乎讀過關於這個主題的每一篇文章,但仍然無法理解它。有人可以幫忙嗎?如何從listView和外部SD中刪除項目/文件?

這是我的代碼和權限已在清單中設置。基本上,當我嘗試使用onLongClick刪除一個項目/文件從列表視圖刪除一個文件,而不是我想要的。該列表中的第一項刪除每一次嘗試。我不知道如何解決這個問題。考慮到文件沒有從SD上的目錄和列表視圖中刪除,我知道代碼是正確的。獲取正確的文件以刪除是問題。任何幫助,將不勝感激。

public class ReadNoteMenu extends ActionBarActivity { 

public static final String EXTRA_MESSAGE = "com.m4tchb0x87.rhinote.MESSAGE"; 

Context context; 

public ReadNoteMenu() { 

    this.context = this; 
} 

ArrayAdapter mArrayAdapter; 
ListView listView; 


protected void onCreate(Bundle bundle) { 
    super.onCreate(bundle); 
    this.setContentView(R.layout.activity_read_note_menu); 

    ActionBar actionBar = getSupportActionBar(); 
    actionBar.setDisplayHomeAsUpEnabled(true); 

    listView = (ListView) this.findViewById(R.id.readListView); 
    String fileNames[] = new File(String.valueOf(Environment.getExternalStorageDirectory().getAbsolutePath()) + "/Rhinote").list(); 
    mArrayAdapter = new ArrayAdapter<>(this, R.layout.list_item_1, fileNames); 
    listView.setAdapter(mArrayAdapter); 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     public void onItemClick(AdapterView adapterView, View view, int i, long l) { 
      String string = (String) listView.getItemAtPosition(i); 
      Intent intent = new Intent(ReadNoteMenu.this, (Class) ReadNote.class); 
      intent.putExtra(EXTRA_MESSAGE, string); 
      ReadNoteMenu.this.startActivity(intent); 
     } 
    }); 

    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 


     public boolean onItemLongClick(AdapterView adapterView, final View view, int position, long l) { 

      ContextThemeWrapper CTW = new ContextThemeWrapper(context, R.style.ADM_theme); 
      MaterialDialogCompat.Builder MDM1 = new MaterialDialogCompat.Builder(CTW); 

      MDM1.setMessage("Edit or delete file?"); 
      MDM1.setPositiveButton("Delete", new DialogInterface.OnClickListener() { 


       public void onClick(DialogInterface dialog, int i) { 

        ContextThemeWrapper CTW = new ContextThemeWrapper(context, R.style.ADM_theme); 
        MaterialDialogCompat.Builder MDM2 = new MaterialDialogCompat.Builder(CTW); 



        MDM2.setMessage("Confirm delete file?"); 
        MDM2.setPositiveButton("Confirm", new DialogInterface.OnClickListener() { 






         public void onClick(DialogInterface dialog, int i) { 


          String str = (String) listView.getItemAtPosition(position); 
          Log.d(str,"Deleted"); 

          new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/RhiNote" + "/" + str).delete(); 

          LayoutInflater inflater = getLayoutInflater(); 
          View layout = inflater.inflate(R.layout.toast_deleted, 
            (ViewGroup) findViewById(R.id.toast_layout_root_deleted)); 
          TextView text = (TextView) layout.findViewById(R.id.text_deleted); 
          text.setText("Note deleted!"); 
          Toast toast = new Toast(getApplicationContext()); 
          toast.setGravity(Gravity.TOP, 250, 25); 
          toast.setDuration(Toast.LENGTH_SHORT); 
          toast.setView(layout); 
          toast.show(); 

          ReadNoteMenu.this.finish(); 
          ReadNoteMenu.this.startActivity(ReadNoteMenu.this.getIntent()); 

         } 
        }); 

        MDM2.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

         public void onClick(DialogInterface dialogInterface, int which) { 

          ReadNoteMenu.this.startActivity(ReadNoteMenu.this.getIntent()); 
         } 
        }); 

        MDM2.show(); 

       } 
      }); 


      MDM1.setNegativeButton("Edit", new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface dialogInterface, int which) { 

        ReadNoteMenu.this.startActivity(ReadNoteMenu.this.getIntent()); 

       } 
      }); 

      MDM1.show(); 


      return true; 
     } 
    }); 
} 

}

回答

0

這裏是我的建議進行調試:

(1)創建從文件列表中選擇您的ArrayList您設置列表適配器之前。

String fileNames[] = new File(String.valueOf(Environment.getExternalStorageDirectory().getAbsolutePath()) + "/Rhinote").list() 
mArrayAdapter = new ArrayAdapter<>(this, R.layout.list_item_1, fileNames); 

在該行調試時暫停並記下每個文件數組中的位置。

(2)然後,在該行再次停頓:

String str = (String)listView.getItemAtPosition(position); 

檢查的位置和字符串匹配你的ArrayList在這一點上。之後,更新您的問題與更多細節。

+0

我對你的建議有點困惑。 ArrayList設置爲File(String.valueOf(Environment.getExternalStorageDirectory()。getAbsolutePath())+「/ Rhinote」)。這是一個包含.txt文件的目錄。你介意給我一個代碼示例嗎?提前致謝。如果我很煩,我很抱歉。 – m4tchb0x 2015-02-09 00:36:58

+0

查看上面的編輯 – Suragch 2015-02-09 10:59:31

+0

我將您的編輯建議添加到了我的代碼中,並設置了日誌。文件夾中有三個.txt文件按此順序設置爲陣列列表... Alpha,Beta,Gamma。當我刪除任何文件時,列表上的第一個文件將以其日誌記錄的形式刪除(02-09 20:59:01.134 29717-29717/com.m4tchb0x87.rhinote D/Alpha.txt:已刪除)。任何進一步的建議? – m4tchb0x 2015-02-10 03:15:36

1

我想通過一些小小的調試後發現錯誤。看來問題在於我沒有聲明onItemLongClick int位置是最終的。

public boolean onItemLongClick(AdapterView adapterView, final View view, final int position, long l) 
+0

您可以通過點擊綠色的勾號來接受您自己的答案.... – 2015-07-05 14:24:32

相關問題