gpt4 book ai didi

android - 如何通过获取它的位置从 SDCard 中删除音频文件

转载 作者:行者123 更新时间:2023-11-29 21:20:38 25 4
gpt4 key购买 nike

我有一个带有复选框的自定义 ListView ,它从 SD 卡文件夹中获取所有音频文件名,当我单击其中任何一个时,它都会播放音频。一切正常。现在当我点击它时我有一个 ImageView ,它显示了带有微调器的 2 个选项

1) 删除
2) 编辑

现在我想要这样,当用户从 ListView 中选择任何复选框然后从此微调器中删除时,我想从 SD 卡和 ListView 中完全删除此音频。所以基本上我想获得 ListView 的位置,即用户点击了哪个项目并想删除它.​​.....

所以请指导我如何实现这一目标。下面是我的代码。

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

媒体路径包含

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

我试过下面的链接,但没有得到我想要的解决方案

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?

最佳答案

我发一个类似的例子希望对你有帮助。无论您在复选框上做什么,我都在删除按钮上做同样的事情。如果你想用复选框来做,那么你必须保持所有复选框的状态或至少最后一个选中的复选框的状态。这是一个自定义适配器,我已经覆盖了 getView 方法。看看我是如何在 ListView 中单击按钮时显示 toast 的,然后您就会明白。如果您仍然遇到任何问题,那么您可以询问。

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

如果您仍然遇到任何问题,那么您可以询问。

关于android - 如何通过获取它的位置从 SDCard 中删除音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20782184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com