Question

I have a custom list-view with check-boxes which get's all audio file name from SD Card folder and when i click any of this it play's audio. Everything is working fine. Now i have a image-view when i click on it, it show's 2 option with spinner

1) Delete
2) Edit

Now i want like this, when user select any check-box from list-view and then delete from this spinner, i want to completely remove this audio from SD Card as well as from List-view. So basically i want to get position of list-view that which item is clicked by user and want to delete it...

So please guide me how can i achieve this. Below is my code.

final Spinner spinnerDelete = new Spinner(Assignment.this);
            alertDialogBuilder.setView(spinnerDelete);

            adapterSpinner = ArrayAdapter.createFromResource(
                    Assignment.this, R.array.delete_menu,
                    android.R.layout.simple_spinner_item);
            adapterSpinner
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinnerDelete.setAdapter(adapterSpinner);

            alertDialogBuilder.setTitle("Delete Data");
            alertDialogBuilder
                    .setMessage("Click yes to Delete Record!")
                    .setCancelable(false)
                    .setPositiveButton("Ok",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    if (spinnerDelete
                                            .getSelectedItemPosition() == 0) {

                                        Code.i = true;
                                        int len = mListView.getCount();
                                        SparseBooleanArray checked = mListView
                                                .getCheckedItemPositions();
                                        for (int i = 0; i < len; i++)
                                            if (checked.get(i)) {
                                                 File file = new File(MEDIAPATH);
                                                 boolean deleted = file.delete();                                                                                                                                   }

where media path contains

private static final String MEDIA_PATH = new String(
        Environment.getExternalStorageDirectory() + "/AudioRecorder/");

I have tried with below link but not getting solution as i want

How to delete a file from SD card?

Delete File in SD Card android with string

Delete file on sd card from a listview

Delete file on sd card from a listview

how to delete a particular file from the paticular folder in sdcard when click on check boxes in list view

How can I make a file to disappear once if it is deleted in android SD card?

Was it helpful?

Solution 4

Just changed below piece of code and now it's working fine, but thank you all.

for (int i = 0; i < len; i++)
    if (checked.get(i)) {
          String delete = myList.get(i);
          File f = new File(MEDIA_PATH + "/"+ delete);
          if (f != null && f.exists()) {
          f.delete();
        }
     }

OTHER TIPS

Similar example I am posting hope it can help you. Whatever you are doing on check box same I am doing on delete button. If you want to do it with check box then you must maintain states of all checkboxes or atleast state of last selected checkbox. This is a custom adapter and I have override getView method. Look how I am display toast on button click in listview then you will understand. If still you are facing any problem then you can ask.

package com.example.list_view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class array_adapter extends ArrayAdapter<String> {
    Context context = null;
    ArrayList<String> file_list = null;
    public array_adapter(Context context, int textViewResourceId,
            ArrayList<String> objects) {
        super(context, textViewResourceId, objects);
        this.context = context;
        this.file_list = objects;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View row_view = convertView;

        if (row_view == null) {
            row_view = View.inflate(context, R.layout.list_layout, null);
            TextView text_view = (TextView) row_view.findViewById(R.id.textView1);
            Button button = (Button) row_view.findViewById(R.id.button1);

            button.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // POSITION YOU HAVE SO YOU CAN DO WHATEVER YOU WANT.
                    String file_name = file_list.get(position);
                    String MEDIA_PATH = new String(Environment.getExternalStorageDirectory() + "/AudioRecorder/" + file_name);
                    File file = new File(MEDIA_PATH);
                    file.delete();
                    file_list.remove(position);
                    notifyDataSetChanged()
                    // ATTACT FILE WITH CORRECT FILE PATH AND DELETE.
                    Toast.makeText(context, file_name, Toast.LENGTH_LONG).show();
                }
            });

            text_view.setText(file_list.get(position));
        }
        return row_view;
    }
}

If you are still facing any problem then you can ask.

Basically your are not going to delete audio or any file. As you have mention in your question that you have Index or Position for each row in list view.

So, Simple is that you have to delete that path as well as that file also.

File file = new File(selectedFilePath);
boolean deleted = file.delete();

Example : Path /sdcard/My_App/sample_audio.mp3

If you have problem with checked item position then go through this

In your "delete" handler you should do the following :

  • Get the positions of selected items, as you already do :

    SparseBooleanArray checked = mListView.getCheckedItemPositions();

  • For each "checked" position :

    1. Construct related file path and remove that file
    2. Remove corresponding data at given position from the adapter

  • After adapter data array/list has been updated and size is reduced
    call notifyDataSetChanged() on your adapter to update mListView

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top