[英]ArrayList<Object> to ArrayList<String>
I have made an ExpandableListView with ArrayList as groupItem and ArrayList as childItem: 我做了一个ExpandableListView,其中ArrayList作为groupItem,而ArrayList作为childItem:
public void setGroupData() {
groupItem.add("TechNology");
groupItem.add("Mobile");
groupItem.add("Manufacturer");
groupItem.add("Extras");
}
ArrayList<String> groupItem = new ArrayList<String>();
ArrayList<Object> childItem = new ArrayList<Object>();
public void setChildGroupData() {
/**
* Add Data For TecthNology
*/
ArrayList<String> child = new ArrayList<String>();
child.add("Java");
child.add("Drupal");
child.add(".Net Framework");
child.add("PHP");
childItem.add(child);
/**
* Add Data For Mobile
*/
child = new ArrayList<String>();
child.add("Android");
child.add("Window Mobile");
child.add("iPHone");
child.add("Blackberry");
childItem.add(child);
/**
* Add Data For Manufacture
*/
child = new ArrayList<String>();
child.add("HTC");
child.add("Apple");
child.add("Samsung");
child.add("Nokia");
childItem.add(child);
/**
* Add Data For Extras
*/
child = new ArrayList<String>();
child.add("Contact Us");
child.add("About Us");
child.add("Location");
child.add("Root Cause");
childItem.add(child);
}
I want the if statement to trigger an action when a child is selected. 我希望if语句在选择子级时触发操作。 However childItem is an object and if I put it like this, nothing happens when I press the "Java" child: 但是childItem是一个对象,如果我这样说,按“ Java”子代时什么也不会发生:
if(childItem.get(childPosition).toString().equals("Java")){
iv.setImageResource(R.drawable.pic);
}
When I tried with groupItem which is a string it worked, but not the way I wanted( it doesn't matter which item I press within group "TechNology" it will work) 当我尝试使用字符串的groupItem时,它起作用了,但不是我想要的方式(我在“ TechNology”组中按下的哪个项都可以起作用)
if(groupItem.get(groupPosition).equals("TechNology")){
iv.setImageResource(R.drawable.pic);
}
What am I doing wrong and how can I fix it? 我在做什么错,我该如何解决?
ArrayList<Object> obj_array = new ArrayList<Object>();
//set this ArrayList ...
ArrayList<String> str_array = new ArrayList<String>();
for (int i = 0; i < obj_array.size(); i++)
{
str_array.add((String)obj_array.get(i));
}
childItem is an array list of array lists, so the toString is calling the toString for an array list. childItem是一个数组列表的数组列表,因此toString正在为数组列表调用toString。 More properly, you can declare childItem as an ArrayList<ArrayList<String>>
object. 更正确地,您可以将childItem声明为ArrayList<ArrayList<String>>
对象。
Your problem is childItem
is an ArrayList of ArrayList of String objects ( List<List<String>>
if you want), so your if
conditions will always return false because you are comparing an ArrayList with a String object. 您的问题是childItem
是String对象的ArrayList的ArrayList(如果需要,则为List<List<String>>
),因此您的if
条件将始终返回false,因为您正在将ArrayList与String对象进行比较。
In order to fix it you should get the corresponding ArrayList<String>
and check the String
instances stored within. 为了修复它,您应该获取相应的ArrayList<String>
并检查其中存储的String
实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.