如何添加子项在ListView [英] How to add subitems in a ListView
本文介绍了如何添加子项在ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的ListView添加子项目。
我的ListView应该为电子邮件项目和他们的子项的机构来组织,但有下列code我刚才添加的项目,我怎么可以加我的子项就可以了?我试过这么多的事情,但它不能正常工作。
列表<登录与GT; listEmails = JsonUtil.getAllEmails(JSON); ArrayList的<串GT;电子邮件=新的ArrayList<串GT;(); ArrayList的<串GT;机构=新的ArrayList<串GT;(); 对(登录loginObj:listEmails){ emails.add(loginObj.getEmailAndress());
} 对(登录loginObj:listEmails){ institutions.add(loginObj.getInstitution());
} ArrayAdapter<串GT;适配器; 适配器=新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_1,电子邮件); emailListView.setAdapter(适配器);
解决方案
要做到这一点,正确的方法是创建每个项目一个HashMap:看看下面的code:
列表<登录与GT; listEmails = JsonUtil.getAllEmails(JSON); ArrayList的<&HashMap的LT;字符串,字符串>>名单=新的ArrayList<&HashMap的LT;字符串,字符串>>(listEmails.size()); 对(登录loginObj:listEmails){ HashMap的<字符串,字符串>项目=新的HashMap<字符串,字符串>();
item.put(电子邮件,loginObj.getEmailAndress());
item.put(机构,loginObj.getInstitution()); list.add(项目);
} 的String [] =由新的String [] {电子邮件,机构}; INT []为= INT新[] {android.R.id.text1,android.R.id.text2}; INT nativeLayout = android.R.layout.two_line_list_item; emailListView.setAdapter(新SimpleAdapter(这一点,列表,nativeLayout,从,到));
I'm trying to add subItems in my ListView. My listView should be organized with emails for items and their institution for the subitems, but with the following code I just added items, how can I add my subitems on it? I've tried so many things but it doesn't work.
List<Login> listEmails = JsonUtil.getAllEmails(json);
ArrayList<String> emails = new ArrayList<String>();
ArrayList<String> institutions = new ArrayList<String>();
for (Login loginObj : listEmails) {
emails.add(loginObj.getEmailAndress());
}
for (Login loginObj : listEmails) {
institutions.add(loginObj.getInstitution());
}
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, emails);
emailListView.setAdapter(adapter);
解决方案
The right way to do that is to create a HashMap for each item: Look the code below:
List<Login> listEmails = JsonUtil.getAllEmails(json);
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(listEmails.size());
for (Login loginObj : listEmails) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("email", loginObj.getEmailAndress());
item.put("institution", loginObj.getInstitution());
list.add(item);
}
String[] from = new String[] { "email", "institution" };
int[] to = new int[] { android.R.id.text1, android.R.id.text2 };
int nativeLayout = android.R.layout.two_line_list_item;
emailListView.setAdapter(new SimpleAdapter(this, list, nativeLayout , from, to));
这篇关于如何添加子项在ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文