如何从一个ListView和外部SD删除项/文件? [英] How to delete item/file from a listView and external SD?

查看:146
本文介绍了如何从一个ListView和外部SD删除项/文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Java和编码都在一起,所以我都碰了壁与我的code。我读过几乎关于这个问题的每一个岗位,仍然不能掌握它。有人能帮忙吗?

I'm new to Java and coding all together so I have hit a wall with my code. I've read nearly every post on this subject and still can't grasp it. Can somebody help?

下面是我的code和权限清单中已设置。基本上,当我尝试使用onLongClick文件列表视图删除项目/文件删除,只是不是我想要的。首先在名单上的项目删除每一个尝试。我不知道如何解决这个问题。我知道,code是说对了一半考虑该文件确实来自于SD目录中,并从列表视图中删除。获得正确的文件删除的问题。任何帮助将是AP preciated。

Here is my code and permissions are already set in the manifest. Basically when I try to delete an item/file from a listview using onLongClick a file deletes, just not the one I want. The item first on the list deletes every attempt. I'm not sure how to solve this. I know that the code is half right considering the file does delete from the directory on the SD and from the listview. Getting the right file to delete is the problem. Any help would be appreciated.

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;
        }
    });
}

}

推荐答案

我想通了,我去一点点调试完毕后错。看来,问题是,我还没有宣布onItemLongClick INT位置决赛。

I figured out where I went wrong after a little debugging. It seems that the problem was that I hadn't declared the onItemLongClick int position final.

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

这篇关于如何从一个ListView和外部SD删除项/文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆