从具有Hashmap值X的Array中获取hashmap

时间:2011-09-15 09:14:38

标签: android arraylist hashmap

我有一个带有多个哈希图的arraylist,其中包含来自sql数据库的信息

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

这个arraylist最终会用项目(人名)填充我的列表视图。 当我点击一个项目时,我想将该hashmap的所有内容发送到另一个页面。 例如:

John&lt;

现在我想将带有John的所有信息的hashmap发送到另一个android页面。 如何从hashmap中获取该信息?

这是我的代码:

public class Contactenlijst extends Activity {

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

    ListView lv;
    lv = (ListView) findViewById(R.id.list);        
    lv.setTextFilterEnabled(true);

    String[] from = new String[] {"naam"};
    int[] to = new int[]{R.id.naam};

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
    // Get the data (see above)
    JSONObject json = Database
            .getJSONfromURL("http://fabian.nostradamus.nu/Android/getcontactinfo.php");

    try {
        JSONArray contactinfo = json.getJSONArray("contactlijst");

        // Loop the Array
        for (int i = 0; i < contactinfo.length(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            JSONObject e = contactinfo.getJSONObject(i);
            map.put("voornaam", e.getString("staff_name"));
            map.put("achternaam", e.getString("staff_lastname"));
            map.put("geboortedatum", e.getString("staff_dateofbirth"));
            map.put("adres", e.getString("staff_address"));
            map.put("postcode", e.getString("staff_address_postal"));
            map.put("woonplaats", e.getString("staff_address_city"));
            map.put("email", e.getString("staff_email"));
            map.put("telefoon", e.getString("staff_phone"));
            mylist.add(map);
        }
    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }

    //array for view
    ArrayList<HashMap<String, String>> mylist2 = new ArrayList<HashMap<String, String>>();
    for(int i = 0; i < mylist.size(); i++){
        HashMap<String,String> map2 = new HashMap<String, String>();
        map2.put("naam", (mylist.get(i).get("voornaam")+" "+(mylist.get(i).get("achternaam"))));
        mylist2.add(map2);
    }
    try{
    lv.setAdapter(new SimpleAdapter(this, mylist2, R.layout.list_item, from, to));
    }catch (Exception e){Log.d("test1","test2");}

    //onclick stuur array naar contactinfo
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
            String text = ((TextView) view).getText().toString();
            Intent i = new Intent(Contactenlijst.this, Contactinfo.class);

            String uittekst[] = text.split(" ");
            String voornaam = uittekst[0].toString();
            String achternaam = uittekst[1].toString();


            startActivity(i);        
        }
      });   
}

}

所有这一切都必须发生在“String achternaam = uittekst [1] .toString();”

1 个答案:

答案 0 :(得分:1)

for(Hashmap<String, String> map: mylist) {
    for(Entry<String, String> mapEntry: map) {
        String key = mapEntry.getKey();
        String value = mapEntry.getValue();
    }
}