android 从列表中删除项目

发布于 2024-11-14 00:50:24 字数 8217 浏览 7 评论 0原文

public class DeleteActivity extends ExpandableListActivity {

    private RingtoneAdapter expListAdapter;
    int myProgress = 0;
    List<String> items = new ArrayList<String>();
    final Context myApp = this;
    // private static final String DIRECTORY = "/system/media/audio/ringtones/";
    private static final String DIRECTORY = "/sdcard/download/";
    private MediaPlayer mp = new MediaPlayer();
    List<String> Ringtones = new ArrayList<String>();
    ArrayAdapter<String> adapter;
    TextView tv, empty;
    ExpandableListView exlv1;
    // ListView lv1;
    Boolean hasErrors = false;
    int currentPosition = 0;
    private static final String LOG_TAG = "MobiIntheMorning";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        refreshList();
        Toast.makeText(this, "hello this is delete called", Toast.LENGTH_LONG).show();

            Ringtones.remove(DIRECTORY+Ringtones.get(1));//THIS DOSE NOT GIVING ANY AFFECT

        refreshList();
        Intent i = new Intent(DeleteActivity.this, FindFilesByType.class);
        startActivity(i);
    }

    public void refreshList() {

        File ringtones_directory = new File(DIRECTORY);
        if (!ringtones_directory.exists()) {
            AlertDialog.Builder ad = new AlertDialog.Builder(
                    DeleteActivity.this);
            ad.setTitle("Directory Not Found");
            ad.setMessage("Sorry! The ringtones directory doesn't exist.");
            ad.setPositiveButton("OK", null);
            ad.show();

            hasErrors = true;
        }

        if ( !ringtones_directory.canRead()) {
            AlertDialog.Builder ad = new AlertDialog.Builder(
                    DeleteActivity.this);
            ad.setTitle("Permissions");
            ad.setMessage("Sorry! You don't have permission to list the files in that folder");
            ad.setPositiveButton("OK", null);
            ad.show();
            hasErrors = true;
        } else {
            Ringtones = FindFiles(false);

            if (Ringtones.size() < 1) {
                AlertDialog.Builder ad = new AlertDialog.Builder(
                        DeleteActivity.this);
                ad.setTitle("Permissions");
                ad.setMessage("Sorry! No ringtones exists in " + DIRECTORY
                        + ".");
                ad.setPositiveButton("OK", null);
                ad.show();
                Log.e(LOG_TAG, "No ringtones were found.");
                hasErrors = true;
            }

        }

        try {

            if ( !hasErrors) {

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                        DeleteActivity.this, android.R.layout.test_list_item,
                        Ringtones);
                ArrayList<String> GrouppList = new ArrayList<String>();
                GrouppList.addAll(Ringtones);

                ArrayList<ArrayList<Color>> colors = new ArrayList<ArrayList<Color>>();
                for (int i = 0; i <= Ringtones.size(); i++) {
                    ArrayList<Color> color = new ArrayList<Color>();

                    color = new ArrayList<Color>();

                    color.add(new Color("", "", true));

                    colors.add(color);
                }

                expListAdapter = new RingtoneAdapter(this, GrouppList, colors);
                Toast.makeText(this, GlobalVariable.getstrEmail(),
                        Toast.LENGTH_LONG).show();
                Ringtones.remove(0);
                // setListAdapter(expListAdapter);

                exlv1 = (ExpandableListView) findViewById(R.id.expandableListView1);
                this.exlv1.setAdapter(this.expListAdapter);

            }
            this.exlv1.setOnGroupExpandListener(new OnGroupExpandListener() {

                @Override
                public void onGroupExpand(int arg0) {
                    Toast.makeText(DeleteActivity.this, "hello" + arg0,
                            Toast.LENGTH_LONG).show();

                    GlobalVariable.SetcurrentPosition(arg0);
                    GlobalVariable.SetstrEmail(DIRECTORY + Ringtones.get(arg0));

                }
            });

        } catch (Exception e) {
            Toast.makeText(this, "Error " + e.toString(), Toast.LENGTH_LONG)
                    .show();
            Log.i(LOG_TAG, e.toString());
        }

    }

    private List<String> FindFiles(Boolean fullPath) {
        final List<String> tFileList = new ArrayList<String>();
        Resources resources = getResources();
        // array of valid audio file extensions
        String[] audioTypes = resources.getStringArray(R.array.audio);
        FilenameFilter[] filter = new FilenameFilter[audioTypes.length];
        int i = 0;
        for (final String type : audioTypes) {
            filter[i] = new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.endsWith("." + type);
                }
            };
            i++;
        }
        FileUtils fileUtils = new FileUtils();
        File[] allMatchingFiles = fileUtils.listFilesAsArray(
                new File(DIRECTORY), filter, -1);
        for (File f : allMatchingFiles) {
            if (fullPath) {
                tFileList.add(f.getAbsolutePath());
            } else {
                tFileList.add(f.getName());
            }
        }
        return tFileList;
    } // find fil

    @SuppressWarnings("unchecked")
    public List<String> loadArray(String filename) {
        try {
            FileInputStream fis = new FileInputStream(filename);
            GZIPInputStream gzis = new GZIPInputStream(fis);
            ObjectInputStream in = new ObjectInputStream(gzis);
            List<String> read_field = (List<String>) in.readObject();
            in.close();
            return read_field;
        } catch (Exception e) {
            e.getStackTrace();
        }
        return null;
    }

    public Collection<File> listFiles(File directory, FilenameFilter[] filter,
            int recurse) {

        Vector<File> files = new Vector<File>();

        File[] entries = directory.listFiles();

        if (entries != null) {
            for (File entry : entries) {
                for (FilenameFilter filefilter : filter) {
                    if (filter == null
                            || filefilter.accept(directory, entry.getName())) {
                        files.add(entry);
                        Log.v(LOG_TAG, "Added: " + entry.getName());
                    }
                }
                if ((recurse <= -1) || (recurse > 0 && entry.isDirectory()))
                    files.addAll(listFiles(entry, filter, recurse - 1));
            }
        }
        return files;

    }

    public class FileUtils {

        public void saveArray(String filename, List<String> output_field) {
            try {
                FileOutputStream fos = new FileOutputStream(filename);
                GZIPOutputStream gzos = new GZIPOutputStream(fos);
                ObjectOutputStream out = new ObjectOutputStream(gzos);
                out.writeObject(output_field);
                out.flush();
                out.close();
            } catch (IOException e) {
                e.getStackTrace();
            }
        }

        public File[] listFilesAsArray(File directory, FilenameFilter[] filter,
                int recurse) {
            Collection<File> files = listFiles(directory, filter, recurse);

            File[] arr = new File[files.size()];
            return files.toArray(arr);
        }
    }
}

我已经完成了一些代码,代码从 SD 卡获取 .mp3 文件并将其显示给我 现在我正在尝试使用它来删除所选项目

Ringtones.remove(DIRECTORY+Ringtones.get(1));

,它应该从列表中删除我的第一个项目,但它对我不起作用
我做错了什么?
我找不到我在这里犯过的任何愚蠢的错误; Ringtones.remove("test.mp3");Ringtones.remove(1.mp3); 我也尝试过。

public class DeleteActivity extends ExpandableListActivity {

    private RingtoneAdapter expListAdapter;
    int myProgress = 0;
    List<String> items = new ArrayList<String>();
    final Context myApp = this;
    // private static final String DIRECTORY = "/system/media/audio/ringtones/";
    private static final String DIRECTORY = "/sdcard/download/";
    private MediaPlayer mp = new MediaPlayer();
    List<String> Ringtones = new ArrayList<String>();
    ArrayAdapter<String> adapter;
    TextView tv, empty;
    ExpandableListView exlv1;
    // ListView lv1;
    Boolean hasErrors = false;
    int currentPosition = 0;
    private static final String LOG_TAG = "MobiIntheMorning";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        refreshList();
        Toast.makeText(this, "hello this is delete called", Toast.LENGTH_LONG).show();

            Ringtones.remove(DIRECTORY+Ringtones.get(1));//THIS DOSE NOT GIVING ANY AFFECT

        refreshList();
        Intent i = new Intent(DeleteActivity.this, FindFilesByType.class);
        startActivity(i);
    }

    public void refreshList() {

        File ringtones_directory = new File(DIRECTORY);
        if (!ringtones_directory.exists()) {
            AlertDialog.Builder ad = new AlertDialog.Builder(
                    DeleteActivity.this);
            ad.setTitle("Directory Not Found");
            ad.setMessage("Sorry! The ringtones directory doesn't exist.");
            ad.setPositiveButton("OK", null);
            ad.show();

            hasErrors = true;
        }

        if ( !ringtones_directory.canRead()) {
            AlertDialog.Builder ad = new AlertDialog.Builder(
                    DeleteActivity.this);
            ad.setTitle("Permissions");
            ad.setMessage("Sorry! You don't have permission to list the files in that folder");
            ad.setPositiveButton("OK", null);
            ad.show();
            hasErrors = true;
        } else {
            Ringtones = FindFiles(false);

            if (Ringtones.size() < 1) {
                AlertDialog.Builder ad = new AlertDialog.Builder(
                        DeleteActivity.this);
                ad.setTitle("Permissions");
                ad.setMessage("Sorry! No ringtones exists in " + DIRECTORY
                        + ".");
                ad.setPositiveButton("OK", null);
                ad.show();
                Log.e(LOG_TAG, "No ringtones were found.");
                hasErrors = true;
            }

        }

        try {

            if ( !hasErrors) {

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                        DeleteActivity.this, android.R.layout.test_list_item,
                        Ringtones);
                ArrayList<String> GrouppList = new ArrayList<String>();
                GrouppList.addAll(Ringtones);

                ArrayList<ArrayList<Color>> colors = new ArrayList<ArrayList<Color>>();
                for (int i = 0; i <= Ringtones.size(); i++) {
                    ArrayList<Color> color = new ArrayList<Color>();

                    color = new ArrayList<Color>();

                    color.add(new Color("", "", true));

                    colors.add(color);
                }

                expListAdapter = new RingtoneAdapter(this, GrouppList, colors);
                Toast.makeText(this, GlobalVariable.getstrEmail(),
                        Toast.LENGTH_LONG).show();
                Ringtones.remove(0);
                // setListAdapter(expListAdapter);

                exlv1 = (ExpandableListView) findViewById(R.id.expandableListView1);
                this.exlv1.setAdapter(this.expListAdapter);

            }
            this.exlv1.setOnGroupExpandListener(new OnGroupExpandListener() {

                @Override
                public void onGroupExpand(int arg0) {
                    Toast.makeText(DeleteActivity.this, "hello" + arg0,
                            Toast.LENGTH_LONG).show();

                    GlobalVariable.SetcurrentPosition(arg0);
                    GlobalVariable.SetstrEmail(DIRECTORY + Ringtones.get(arg0));

                }
            });

        } catch (Exception e) {
            Toast.makeText(this, "Error " + e.toString(), Toast.LENGTH_LONG)
                    .show();
            Log.i(LOG_TAG, e.toString());
        }

    }

    private List<String> FindFiles(Boolean fullPath) {
        final List<String> tFileList = new ArrayList<String>();
        Resources resources = getResources();
        // array of valid audio file extensions
        String[] audioTypes = resources.getStringArray(R.array.audio);
        FilenameFilter[] filter = new FilenameFilter[audioTypes.length];
        int i = 0;
        for (final String type : audioTypes) {
            filter[i] = new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.endsWith("." + type);
                }
            };
            i++;
        }
        FileUtils fileUtils = new FileUtils();
        File[] allMatchingFiles = fileUtils.listFilesAsArray(
                new File(DIRECTORY), filter, -1);
        for (File f : allMatchingFiles) {
            if (fullPath) {
                tFileList.add(f.getAbsolutePath());
            } else {
                tFileList.add(f.getName());
            }
        }
        return tFileList;
    } // find fil

    @SuppressWarnings("unchecked")
    public List<String> loadArray(String filename) {
        try {
            FileInputStream fis = new FileInputStream(filename);
            GZIPInputStream gzis = new GZIPInputStream(fis);
            ObjectInputStream in = new ObjectInputStream(gzis);
            List<String> read_field = (List<String>) in.readObject();
            in.close();
            return read_field;
        } catch (Exception e) {
            e.getStackTrace();
        }
        return null;
    }

    public Collection<File> listFiles(File directory, FilenameFilter[] filter,
            int recurse) {

        Vector<File> files = new Vector<File>();

        File[] entries = directory.listFiles();

        if (entries != null) {
            for (File entry : entries) {
                for (FilenameFilter filefilter : filter) {
                    if (filter == null
                            || filefilter.accept(directory, entry.getName())) {
                        files.add(entry);
                        Log.v(LOG_TAG, "Added: " + entry.getName());
                    }
                }
                if ((recurse <= -1) || (recurse > 0 && entry.isDirectory()))
                    files.addAll(listFiles(entry, filter, recurse - 1));
            }
        }
        return files;

    }

    public class FileUtils {

        public void saveArray(String filename, List<String> output_field) {
            try {
                FileOutputStream fos = new FileOutputStream(filename);
                GZIPOutputStream gzos = new GZIPOutputStream(fos);
                ObjectOutputStream out = new ObjectOutputStream(gzos);
                out.writeObject(output_field);
                out.flush();
                out.close();
            } catch (IOException e) {
                e.getStackTrace();
            }
        }

        public File[] listFilesAsArray(File directory, FilenameFilter[] filter,
                int recurse) {
            Collection<File> files = listFiles(directory, filter, recurse);

            File[] arr = new File[files.size()];
            return files.toArray(arr);
        }
    }
}

I've done some code that code fetches .mp3 files from the sdcard and displays it to me
now i am trying to delete selected item from it using

Ringtones.remove(DIRECTORY+Ringtones.get(1));

that is it should delete my first item from the list but it doesn't works for me
what is wrong i ma doing into this?
I am not able to find any silly mistakes I made here; Ringtones.remove("test.mp3"); and Ringtones.remove(1.mp3); both I've also tried.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空‖城人不在 2024-11-21 00:50:24

你为什么在做

目录 + Ringtones.get(1)

Ringtones 是一个列表,因此您想要执行类似的操作

 Ringtones.remove(1);

,或者

Ringtones.remove(aRintoneString)

您在这里有效执行的操作是获取第一个元素并添加其他字符串,这将为您提供 Ringtones 中不存在的新第三个字符串

Why are you doing

DIRECTORY + Ringtones.get(1)

Ringtones is a List so you want to be doing something like

 Ringtones.remove(1);

or

Ringtones.remove(aRintoneString)

what you effectively doing here is getting the first element and adding a other string which gives you a NEW third string that doesn't exist in Ringtones

夏末染殇 2024-11-21 00:50:24

您好,从列表中删除项目后,在适配器上调用notifyDataSetChanged()。
它将刷新您的列表,您将在列表中获得更新的项目。

Hi Call notifyDataSetChanged() on your Adapter after removing the item from the List.
It will refresh your List and you will get updated items in your list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文