-5

I wish to pass data from one Listview activity to the next activity, i.e. when an item is clicked in the list, data such as the title or thumb_url (an image from a remote database) can be passed to a detail Activity.

In the example below, these items are set out in an HashMap.

What code will pass the above data to another activity, using an intent?

I'm puzzled because these data items are laid out in a HashMap.

public class CustomizedListView extends Activity {
    // All static variables
    static final String URL = "http://padihamcars.com/music.xml";
    // XML node keys
    static final String KEY_SONG = "song"; // parent node
    static final String KEY_ID = "id";
    static final String KEY_TITLE = "title";
    static final String KEY_ARTIST = "artist";
    static final String KEY_DURATION = "duration";
    static final String KEY_THUMB_URL = "thumb_url";

    ListView list;
    LazyAdapter adapter;

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

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

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_SONG);
        // looping through all song nodes <song>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
            map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
            map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
            map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

            // adding HashList to ArrayList
            songsList.add(map);
        }

        list=(ListView)findViewById(R.id.list);

        // Getting adapter by passing xml data ArrayList
        adapter=new LazyAdapter(this, songsList);        
        list.setAdapter(adapter);

        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
}
Jitesh Prajapati
  • 2,533
  • 4
  • 29
  • 51
JohnFerg
  • 11
  • 4
  • 1
    Please refer this http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android – Anand Savjani Sep 07 '16 at 11:54
  • It's simpler than you expect. Just do the reverse of what you've done before `songsList.add(map);`. – Sufian Sep 07 '16 at 12:04
  • Your question was unclear, possibly because you were not breaking it down to a more solvable task. Passing it to the Activity comes after you have got the object. Besides, you should use a [POJO](http://stackoverflow.com/questions/3527264/how-to-create-a-pojo) to avoid messy code in the future. – Sufian Sep 07 '16 at 12:06
  • Possible duplicate of [How to send hashmap value to another activity using an intent](http://stackoverflow.com/questions/7578236/how-to-send-hashmap-value-to-another-activity-using-an-intent) – Sufian Sep 07 '16 at 12:10

2 Answers2

1

Try this :-

 list.setOnItemClickListener(new OnItemClickListener() {

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

                        HashMap<String, String> map  = songsList.get(position);
                        Intent intent = new Intent(YourActivity.this, MyOtherActivity.class);
                        intent.putExtra("map", map);
                        startActivity(intent);

                }

And than in the receiving Activity:

protected void onCreate(Bundle bundle) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    HashMap<String, String> hashMap = (HashMap<String, String>)intent.getSerializableExtra("map");
    Log.v("HashMapTest", hashMap.get("key"));
}
Vishal Patoliya ツ
  • 3,170
  • 4
  • 24
  • 45
  • Thank you. Not sure if this will retrieve the values in the second activity, though - "key" is a bit vague.. – JohnFerg Sep 07 '16 at 12:40
1

Try this way:Fetch the hashmap of a particular position from the list and then fetch the values using the keys.

list.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
    HashMap<String, String> resultp = new HashMap<String, String>();
    resultp   = songsList.get(position);
    String s_id=resultp.get(KEY_ID);
    String s_title=resultp.get(KEY_TITLE);
    Intent intent = new Intent(YourActivity.this, MyOtherActivity.class);
    intent.putExtra("s_id", s_id);
    intent.putExtra("s_title", s_title);
    //other fields you want to send
    startActivity(intent);
}

Define the arraylist: ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>(); globally.

kgandroid
  • 5,507
  • 5
  • 39
  • 69