I have done with code in Android:
List<String> spinnerArray = new ArrayList<String>();
for (int i = 0; i < items.size(); i++) {
LinkedTreeMap<String, String> item = (LinkedTreeMap) items.get(i);
// THIS LINE THROW EXCEPTION
double d = Double.parseDouble(item.get("id").toString());
locations.put(Integer.parseInt(item.get("id")), item.get("name"));
spinnerArray.add(item.get("name"));
}
I get the error when I define the "d" variable.
The strange thing is if I run the same line in the debug I get the double value I need.
UPDATE ANSWER
On the debug window I have:
items.get(i) = {LinkedTreeMap@5317} size = 5
0 = {LinkedTreeMap$Node@5378} "id" -> "1.0"
key = "id"
value = {Double@5384} "1.0"
Seems that LinkedTreeMap items
is of type [String, Object] and not [String, String].
So your code should look like:
LinkedTreeMap<String, Object> item = (LinkedTreeMap) items.get(i);
Thanks to Timothy Truckle.
The long term solution to your problem is to define a custom class to be used as Data Transfer Object (DTO) where you cann access an items properties in a typesafe way:
class Item {
private double id;
private String name;
public void setId(double id){this.id=id;}
public double getId(){ return id;}
public void setName(String name){this.name = name;}
public String getName(){return name;}
}
usage:
for (int i = 0; i < items.size(); i++) {
Item item = (LinkedTreeMap) items.get(i);
// THIS LINE THROW EXCEPTION
double d = item.getId();
locations.put((int)item.getId(), item.getName());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.