java - 如何从 Arraylist 获取字符串?

标签 java string sqlite dictionary 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 检索值,您需要它的键。

之前在您的代码中,您使用...生成了 map

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

这会给你 key 。要检索值,您需要使用适当的 key ,例如...

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

您可能想查看 The Map Interface了解更多详情

关于java - 如何从 Arraylist 获取字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21395762/

相关文章:

java - Sqlite 连接 ruby​​ java 桥

Java 级联枚举?

java - 如何检测用户输入文本的语言?

Java Jframe 向另一个 X Server 启动另一个 JFrame

python - 你如何在 Python 中可视化一个巨大的整数?

sqlite - 关于sqlite中的算术

java - 在java中锁定 `properties`文件

c# - 检测字符串是否全部大写

c - 请解释 sbegin = s ? s : *lasts;

android - 如何在 Android 的 OnDestroy() 中使 SQLite 数据库上下文为空?