繁体   English   中英

如何从Arraylist中获取字符串?

[英]How do I get string from Arraylist ?

我有一个Globals.mylist=new arraylist<hashmap<String,String>> mylist和hashmap map1来保持我的产品订单,然后再将它们插入数据库。 这部分是当我将产品订单插入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);
}

而这部分是当我尝试将字符串从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);
}

但是我得到一个错误。 它告诉我Id=0 and qty=null 我尝试搜索参考,但是无法实现它们。 如何从Globals.mylist获取字符串ID和字符串数量以插入sqlite。

要从Map检索值,您需要键。

以前在您的代码中,您使用...生成地图。

map1.put("one", Id);
map1.put("two", brand);
map1.put("three", type);
map1.put("four", qty); 

给你钥匙。 要检索值,您需要使用适当的键,例如...

map1=Globals.mylist.get(i);
String id = map1.get("one");
String qty = map1.get("four");

您可能想通过地图界面了解更多详细信息

使用LinkedHashMap可以简化插入顺序的设计,它会容易得多。

首先,我认为我们没有足够的代码。 但是.get(int)函数返回一个对象,它实际上不执行任何操作,因此您需要执行诸如System.out.println(map1.get(Id))之类的操作。

你的问题是,你正在试图获得value使用其地图的value是不是它的实现方式。

map1.get(Id);
map1.get(qty);

不正确的方法。


您需要在需要检索其valuekey上调用Map#get 您将获得声明的String类型的值,因此可以在相同类型的变量中对其进行检索。

String Id = map1.get("one");
String qty = map1.get("four");

正确的调用方式。

在添加要映射的内容时,可以使用键值对。

map.put(key,value);

当您想获取字符串时,应该使用;

map.get(键);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM