简体   繁体   中英

Adding items to an array list by clicking on an item from an arraylist in another activity

I know I am Probably making a bigger deal out of this but here is my problem:

I basically want to be able to make a playlist from a list of songs. As each song/item is clicked it adds it to another arraylist in another activity.

Please help it so frustrating:

here is my code..

public class SDLTlist extends ListActivity {

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


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ltsdlist);

        ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();

        ImportSD plm = new ImportSD();
        // get all songs from sdcard
        this.songsList = plm.getPlayList();

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

            // adding HashList to ArrayList
            songsListData.add(song);
        }

        // Adding menuItems to ListView
        ListAdapter adapter = new SimpleAdapter(this, songsListData,
                R.layout.playlist_item, new String[] { "songTitle" }, new int[] {
                        R.id.songTitle });

        setListAdapter(adapter);
        };

     });

   }
}

The above class imports songs using MEDIA_PATH with file extensions .MP3 (which is called in another class called importSD) and lists them in an array, as I select an item from that list I want it to be added to another list in a different activity. Should I use the onItemClickListners function? If so how would I do it?

You can either:

A. Keep the ArrayList you want to add to outside the Activities ( ie. In a class that overrides Application ) and then add it while still in the second Activity.

or

B. Pass the selected Array of data back to the Activity and handle it in OnActivityResult by pulling out the list you passed back.

IE. If you have a String[], you could pull that out of the passed-back Intent with:

data.getStringArrayExtra("my_string_array_name")

You can always create a global application class that can be shared across all your activities:

package com.example.app;

import android.app.Application;
import java.util.ArrayList;

public class GlobalVars extends Application {

    public ArrayList<String> aList;

    public GlobalVars() {
        aList = new ArrayList<String>();
    }

}

Then in each Activity you can get the global class like this:

gVars = (GlobalVars) getApplication();
gVars.aList.add("Adding a String");

Make sure to add this Application class correctly to your AndroidManifest.xml.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM