gpt4 book ai didi

android - 从 Hashmap 值为 X 的数组中获取 hashmap

转载 作者:行者123 更新时间:2023-11-30 04:32:40 25 4
gpt4 key购买 nike

我有一个包含多个 HashMap 的数组列表,其中包含来自 sql 数据库的信息

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

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

约翰 <

现在我想将包含 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();”下发生

最佳答案

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

关于android - 从 Hashmap 值为 X 的数组中获取 hashmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7428519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com