2015-02-06 83 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

相关问题