开发者

Get hashmap out of an Array with Hashmap value X

开发者 https://www.devze.com 2023-04-04 20:29 出处:网络
I have an arraylist with multiple hashmaps that contains information that comes from a sql database ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String&

I have an arraylist with multiple hashmaps that contains information that comes from a sql database

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

This arraylist will eventually fill my listview with items (names of people). When I click on one item I want to send all of the content of that hashmap to another page. For example:

John <

Now I want to send the hashmap with all the information of John to another android page. How do I get that information out of the hashmap?

This is my code:

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_e开发者_如何学编程mail"));
            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);        
        }
      });   
}

}

So it all has to happen under "String achternaam = uittekst[1].toString();"


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号