删除文件后试图刷新ListView

时间:2014-05-14 15:06:47

标签: android listview android-listview hashmap listadapter

在我的List2活动(扩展ListActivity)中,我删除了一个文件,之后我调用方法init();来刷新我的ListView,但它并不令人耳目一新,它只是重复(出现旧的和新的)项目。 如果我点击其中一个新生成的项目,它将强行关闭。我知道notifyDatasetChanged在我的案例中不起作用。

任何帮助都将不胜感激。

这是我的List2类:

    public class List2 extends ListActivity {

    ListView lv;
    private ListAdapter adapter;

    public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    public ArrayList<HashMap<String, String>> myHash = new ArrayList<HashMap<String, String>>();

    // private ArrayList<DataSetObserver> observers = new
    // ArrayList<DataSetObserver>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list2);

        init();

        lv = getListView();

        registerForContextMenu(getListView());

        // listening to single listitem click
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    final int position, long id) {

                // // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        secondActivity.class);

                // Sending songIndex to PlayerActivity
                in.putExtra("Index", fileIndex);

                // setResult(100, in);
                // Closing PlayListView
                startActivity(in);
                finish();
            }
        });

    }

    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);

    }

    public boolean onContextItemSelected(MenuItem item) {
        final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
                .getMenuInfo();

        switch (item.getItemId()) {

        case R.id.delete:

            File file = new File(Path);
            if (file.exists()) {

                file.delete();

                init();

            }

            return true;
        default:
            return super.onContextItemSelected(item);
        }
    }

    public ArrayList<HashMap<String, String>> getPlayList() {
        File home = new File(MEDIA_PATH);

        if (home.listFiles(new FileExtensionFilter()).length > 0) {
            for (File file : home.listFiles(new FileExtensionFilter())) {
                HashMap<String, String> song = new HashMap<String, String>();

                song.put(
                        "songTitle",
                        file.getName().substring(0,
                                (file.getName().length() - 4)));
                song.put("songPath", file.getPath());

                songsList.add(song);
            }
        }

        // return songs list array
        return songsList;
    }

    void init() {

        this.getPlayList();

        // looping through playlist
        for (int i = 0; i < songsList.size(); i++) {
            // creating new HashMap
            HashMap<String, String> song = songsList.get(i);

            myHash.add(song);
        }
        adapter = new SimpleAdapter(this, myHash, R.layout.playlist_item,
                new String[] { "songTitle", "singerName" }, new int[] {
                        R.id.songTitle, R.id.singerName });

        setListAdapter(adapter);

    }

}

1 个答案:

答案 0 :(得分:0)

它是因为您在不清除它的情况下添加myHash,因此它包含所有旧条目以及新条目。只需在向其添加更多项目之前致电clear

void init() {

this.getPlayList();
myHash.clear();

// looping through playlist
for (int i = 0; i < songsList.size(); i++) {

    // creating new HashMap
    HashMap<String, String> song = songsList.get(i);

    myHash.add(song);
}

adapter = new SimpleAdapter(this, myHash,
R.layout.playlist_item, new String[] { "songTitle","singerName" }, new int[] {
R.id.songTitle,R.id.singerName });

setListAdapter(adapter);

}