[英]How do I get string from Arraylist ?
I have a Globals.mylist=new arraylist<hashmap<String,String>>
mylist, and hashmap map1 to keep my product order before I insert them into database. 我有一个
Globals.mylist=new arraylist<hashmap<String,String>>
mylist和hashmap map1来保持我的产品订单,然后再将它们插入数据库。 this part was when I insert my product order into Globals.mylist : 这部分是当我将产品订单插入Globals.mylist时:
if (Globals.mylist == null) {
Globals.mylist = new ArrayList<HashMap<String, String>>();
}
id = bun.getLong("id");
Id=String.valueOf(id);
brand = bun.getString("brand");
type = bun.getString("type");
qty = bun.getString("qty");
if (Globals.mylist.size() == 0) {
map1.put("one", Id);
map1.put("two", brand);
map1.put("three", type);
map1.put("four", qty);
Globals.mylist.add(map1);
} else {
int j = Globals.mylist.size();
map1.put("one", Id);
map1.put("two", brand);
map1.put("three", type);
map1.put("four", qty);
Globals.mylist.add(j,map1);
}
And this part was when I try to get string from Globals.mylist into sqlite 而这部分是当我尝试将字符串从Globals.mylist转换为sqlite时
for (int i=0; i<Globals.mylist.size();i++) {
map1=Globals.mylist.get(i);
map1.get(Id);
map1.get(qty);
orderdtl = dataSource.createorderdtl(orderid, Id, qty);
}
But I get an error. 但是我得到一个错误。 It tells me that
Id=0 and qty=null
. 它告诉我
Id=0 and qty=null
。 I try to search reference, but I cant implement them. 我尝试搜索参考,但是无法实现它们。 How can I get string id and string qty from
Globals.mylist
to inserted into sqlite. 如何从
Globals.mylist
获取字符串ID和字符串数量以插入sqlite。
To retrieve a value from a Map
you need it's key. 要从
Map
检索值,您需要键。
Previously in your code, you generated the map using... 以前在您的代码中,您使用...生成地图。
map1.put("one", Id);
map1.put("two", brand);
map1.put("three", type);
map1.put("four", qty);
Which gives you the keys. 给你钥匙。 To retreieve the values, you would need to use the appropriate key, for example...
要检索值,您需要使用适当的键,例如...
map1=Globals.mylist.get(i);
String id = map1.get("one");
String qty = map1.get("four");
You might like to take a look through The Map Interface for more details 您可能想通过地图界面了解更多详细信息
使用LinkedHashMap可以简化插入顺序的设计,它会容易得多。
Firstly, I don't think we have enough code. 首先,我认为我们没有足够的代码。 But the .get(int) function returns an object, it doesn't actually do anything so you'll need to do something like System.out.println(map1.get(Id)).
但是.get(int)函数返回一个对象,它实际上不执行任何操作,因此您需要执行诸如System.out.println(map1.get(Id))之类的操作。
Your problem is that you are trying to get the value
of the Map using its value
which is not the way it is implemented. 你的问题是,你正在试图获得
value
使用其地图的value
是不是它的实现方式。
map1.get(Id);
map1.get(qty);
Not the right way. 不正确的方法。
You need to call Map#get
on the key
whose value
you need to retrieve. 您需要在需要检索其
value
的key
上调用Map#get
。 You will get the value of type String
as you have declared it so retrieve it in the variable of the same type. 您将获得声明的
String
类型的值,因此可以在相同类型的变量中对其进行检索。
String Id = map1.get("one");
String qty = map1.get("four");
The right way to call it. 正确的调用方式。
While adding something to map, you use key value pair. 在添加要映射的内容时,可以使用键值对。
map.put(key, value); map.put(key,value);
when you want to get string you should use; 当您想获取字符串时,应该使用;
map.get(key); map.get(键);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.