android从列表中删除项目

时间:2011-06-03 10:10:19

标签: android expandablelistview expandablelistadapter

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

我做了一些代码,代码从sdcard中获取.mp3文件并将其显示给我 现在我正在尝试使用

从中删除所选项目
Ringtones.remove(DIRECTORY+Ringtones.get(1));

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

2 个答案:

答案 0 :(得分:1)

你为什么要这样做

  

DIRECTORY + Ringtones.get(1)

铃声是一个列表,所以你想做一些像

这样的事情
 Ringtones.remove(1);

Ringtones.remove(aRintoneString)

你在这里有效地做的是获得第一个元素并添加另一个字符串,它给你一个在铃声中不存在的新的第三个字符串

答案 1 :(得分:0)

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