简体   繁体   English

如何从Arraylist中获取字符串?

[英]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. 您需要在需要检索其valuekey上调用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.

相关问题 如何返回ArrayList <String> 来自ArrayList <ArrayList<String> &gt;获得() - How do I return an ArrayList<String> from ArrayList<ArrayList<String>>.get() 如何从ArrayList的ListView上获取String值 <String[]> ? - How do I get String values on listview from the ArrayList<String[]>? 如何从ArrayList获取String [] <String[]> ,我实际上想从ArrayList的索引0获得String <String[]> ? - How to get String[] from ArrayList<String[]>, I want to actually get String at index 0 from ArrayList<String[]>? 如何从 ArrayList 中获取元素<ArrayList<String[]> ? - How to get an element from an ArrayList<ArrayList<String[]>? 如何从 ArrayList() 或 ArrayList 转换对象<String>到 ArrayList<Integer> ? - How do I convert an object from an ArrayList() or ArrayList<String> to ArrayList<Integer>? 如何将文本文件中的String元素添加到ArrayList中? - How do I add String elements from a text file into an ArrayList? 如何检索ArrayList的键 <String> 来自Hashtable? - How do I retrieve a key of ArrayList<String> from a Hashtable? 如何从 ArrayList 中检索特定值<string>在 Spring</string> - How do I retrieve particular value from an ArrayList<String> in Spring 如何用arraylist中的多个匹配项替换字符串? - How do I replace a string with multiple matches from an arraylist? 我如何返回ArrayList <String> 从方法上? - How do i return an ArrayList<String> from a method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM