0
/*Files will be clicked here*/
@Override
    protected void onListItemClick(ListView l,View v, final int position, long id){
    filePosition=new File(path1.get(position));
    System.out.println("File position:"+filePosition);
    if(filePosition.isDirectory()){
    if(filePosition.canRead())
            getDirectories(path1.get(position)); 
else
        {
new AlertDialog.Builder(VideoSDcard.this)
            .setIcon(R.drawable.folder)
            .setTitle("["+filePosition.getName()+"]folder can't be read!")
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                }
            }).show();

        }

    }
else {

        AlertDialog.Builder alert=new AlertDialog.Builder(this);
        alert.setIcon(R.drawable.folder);
        alert.setTitle("Do you want to Delete this File from SD card ?"+" [ "+filePosition.getName()+" ] ");

                  //.setTitle("["+filePosition.getName()+"]")
         alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                /*Deleting a File*/

                deleteFile(); // here deleting a file


            }

            private void deleteFile() {

                  boolean deleted= filePosition.delete();
    }


        });

我正在浏览 SD 卡中的文件和目录。所以我通过使用列表视图来显示它。当我不需要 sd 卡上的文件时,我可以删除文件。

我的问题是,在我删除一个文件后,它已在 SD 卡上完全删除,但它在 listview 上可见。我的意思是在模拟器中。从列表中删除文件后,它应该会消失。

例如:- 我在 sd 卡上有 song.mp4 文件。一旦我删除了这个文件“song.mp4”,它就不应该再次显示在列表视图中。

我该怎么做?

4

2 回答 2

0

添加这一行

adapter.notifyDataSetChanged();

deleteFile()onclick事件中的方法..

或者

之后使用意图调用相同的活动..

试试这样:

adapter = new MyListAdapter(this);
    lv = (ListView) findViewById(android.R.id.list);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> a, View v, int position, long id) {
        AlertDialog.Builder adb=new AlertDialog.Builder(MyActivity.this);
        adb.setTitle("Delete?");
        adb.setMessage("Are you sure you want to delete " + position);
        final int positionToRemove = position;
        adb.setNegativeButton("Cancel", null);
        adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                MyDataObject.remove(positionToRemove);
                adapter.notifyDataSetChanged();
            }});
        adb.show();
        }
    });
于 2013-06-07T11:13:54.273 回答
0

您可以创建一个函数来更新您的 listdataitem,并在从 sdcard 中删除文件后调用此函数。它将引用您的 listItem。您可以第一次调用相同的函数来设置数据列表。

活动类.java

ListView listview;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listview = (ListView) findViewById(android.R.id.list); 
    refreshData();
    listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {

            //show your dialogbox here
            //and on click of dialog's delete button referesh listview adapter and set it in listview.
            .
            .
             AlertDialog.Builder alert=new AlertDialog.Builder(this);
             alert.setIcon(R.drawable.folder);
             alert.setTitle("Do you want to Delete this File from SD card ?"+" [ "+filePosition.getName()+" ] ");

              //.setTitle("["+filePosition.getName()+"]")
             alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            /*Deleting a File*/

            deleteFile(); // here deleting a file
           refreshData();//refersh listItem


        }

        private void deleteFile() {

              boolean deleted= filePosition.delete();
}




        }
    });





}

public void refreshData(){


    ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
     listview.setAdapter(adapter);

}
于 2013-06-07T11:47:28.513 回答