[英]how to get two string array item position and store in each separate string array in android
對不起,我的英語不好。 我為arraylist的每個項目使用兩個單獨的字符串數組。 一切正常。 我的問題是,如果我采用字符串數組“ asasaqty”和另一項字符串數組“ asasaId”,那么我想采用String數組的每一項(例如),然后我要將字符串數組值的每一項存儲在第三個字符串數組中(例如: [id1,qty1]等。 請讓我知道是否有適當的解決方案。
下面是我的代碼。
提前致謝
String [] asasaqty;
String [] asasaId;
final AddToCartData addToCartData = response.body();
shoppingBagArrayList = addToCartData.getData();
asasaqty = new String[shoppingBagArrayList.size()];
asasaId = new String[shoppingBagArrayList.size()];
for (int i = 0; i < shoppingBagArrayList.size(); i++) {
asasaqty[i] = shoppingBagArrayList.get(i).getQty();
asasaId[i]=shoppingBagArrayList.get(i).getProductId();
System.out.println("==========wwwww========"+ Arrays.toString(asasaqty));
System.out.println("======wwwwssssswwwwss======"+ Arrays.toString(asasaId));
/* HashMap<String,String> map=new HashMap<String,String>();
for (int i = 0; i < asasaqty.length; i++) {
map.put(asasaqty[i],asasaId[i]);
System.out.println("======aaaaaaa======"+ map.put(asasaqty[i],asasaId[i]));
}*/
在logcat中打印值后,該值看起來像
/System.out: ==========wwwww========[3, 2]
/System.out: ======wwwwssssswwwwss======[151, 10]
現在,我想獲取另一個String數組,並將每個項目asasaqty和asasaId存儲在單獨的數組中,如下所示
String[] xyz = [3,151] and
String[] abc = [2,10]
請讓我知道我們該怎么做。
您可以使用productID和鍵(假設它是唯一的)和數量作為值。
HashMap<Integer,Integer> dataMap = new HashMap<>();
for (int i=0; i <shoppingBagArrayList.size; i++){
dataMap.put(shoppingBagArrayList.get(i).getProductId() , shoppingBagArrayList.get(i).getQty());
}
System.out.println("DataMap: "+dataMap);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.