如何从HashMap获取数据以传递给下一个Activity?

时间:2016-09-07 11:52:59

标签: android listview android-intent hashmap

我希望将数据从一个Listview活动传递到下一个活动,即当在列表中单击某个项目时,可以将诸如title或thumb_url(来自远程数据库的图像)之类的数据传递给详细活动。

在下面的示例中,这些项目在HashMap

中列出

什么代码会使用意图将上述数据传递给另一个活动?

我很困惑,因为这些数据项都是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) {
}

2 个答案:

答案 0 :(得分:1)

试试这个: -

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

                }

而不是接收活动:

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

答案 1 :(得分:1)

尝试这种方式:从列表中获取特定位置的哈希映射,然后使用键获取值。

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

全局定义arraylist:ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();