user3144107
user3144107

Reputation:

List duplicates itself after some time

Here is the scenario.

1) I have a listview which generates a list of all the songs (which is a fragment)

2) Now upon clicking upon any song, a new activity is launched.

3) Here is the problem I face,upon coming back from the new activity after some time(mind it, it doesn't happen when I come back instantly) I find the list has repeated it self.
And when I click on any item, it doesn'w work.

I have no clue why this happens and what I can do it avoid it.

code :

public class F_Songs extends Fragment implements Serializable {

    ListView SngList;

    ArrayList<SongDetails> songdetails = new ArrayList<SongDetails>();
    String[] TRACK_COLUMNS;
    Cursor songCursor = null;
    Adapter_ListView ab;
    int index;
    MediaMetadataRetriever mmr = new MediaMetadataRetriever();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = (ViewGroup) inflater.inflate(R.layout.l_songs, null);
        TRACK_COLUMNS = new String[] { MediaStore.Audio.AudioColumns.ALBUM, MediaStore.Audio.AudioColumns.ARTIST,
                MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME,
                MediaStore.Audio.AudioColumns.DURATION, MediaStore.Audio.Media._ID, };
        String sortOrder = "REPLACE ('<BEGIN>' || " + MediaStore.Audio.Media.TITLE + ", '<BEGIN>The ', '<BEGIN>')"
                + " COLLATE NOCASE ASC";
        // MediaStore.Audio.Media.TITLE + " COLLATE NOCASE ASC";
        Cursor songCursor = getActivity().managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, TRACK_COLUMNS,
                MediaStore.Audio.Media.DURATION + ">= 10000", null, sortOrder);
        if (songCursor != null) {
            while (songCursor.moveToNext()) {
                SongDetails songs = new SongDetails();
                songs.song = songCursor.getString(8);
                songs.Path = songCursor.getString(2);
                songs.Album = songCursor.getString(0);
                songs.Artist = songCursor.getString(1);
                songdetails.add(songs);
            }
        }
        SngList = (ListView) view.findViewById(R.id.list);
        registerForContextMenu(SngList);
        /*
         * String uri = "android.resource://" +
         * this.getActivity().getPackageName() + "/"+R.raw.ro; SongDetails
         * songs = new SongDetails(); songs.setPath2( uri) ;
         * songs.setSong("ro"); songdetails.add(songs);
         */
        SngList.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView a, View v, int position, long id) {
                Intent intent = new Intent(getActivity(), Player.class);
                intent.putParcelableArrayListExtra("Data1", songdetails);
                intent.putExtra("Data2", position);
                startActivity(intent);
            }
        });
        ab = new Adapter_ListView(songdetails);
        SngList.setAdapter(ab);

    }
}

Upvotes: 0

Views: 79

Answers (1)

Ilya Gazman
Ilya Gazman

Reputation: 32271

It's hard to find where the problem comes from. My guess will be that it's because of this line

ArrayList<SongDetails> songdetails = new ArrayList<SongDetails>();

Outside the onCreateView method.

Try improve your data managing using MVC pattern and Singleton, like I suggest you in the previous question.

Upvotes: 1

Related Questions