Question

In my List2 activity (extending ListActivity), I am deleting a file and after that I call the method init(); to refresh my ListView but it's not refreshing, it's only duplicating (appearing old and new ones) items. And if I click on one of those new generated items it will force close. I know notifyDatasetChanged doesn't work in my case.

Any help would be appreciated.

This is my List2 Class :

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

    }

}
Was it helpful?

Solution

Its because you are adding myHash without clearing it, so it contains all of the old entries as well as the new ones. Simply call clear before you add more items to it.

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

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