[英]how to store a arrayList to Map 'string as key and how to retrieve it..!
i wanna retrieve map(ArrayList) according to the key, help me with the code below 我想根据键检索map(ArrayList),请使用以下代码帮助我
public class MainActivity extends AppCompatActivity {
Map<String,ArrayList<Model>> map=new ArrayMap<>(); <----
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......
.....
for (int j=0;j<jsonArray.length();j++)
{
JSONObject tempObj=jsonArray.getJSONObject(j);
String price =tempObj.getString("price");
String product=tempObj.getString("Product");
String qty=tempObj.getString("Qty");
modelList.add(new Model(id,price,product,qty,date));
map.put(key,modelList); <----
modelList.clear();
}
......
......
//here to retrieve that map in the same mainactivity
CODE....??????? <----
here is my json where those months are dynamic (jan, april, jun,...they are not constant). 这是我的json,这些月份是动态的(jan,pril,jun,...它们不是恒定的)。
{
"response": "success",
"servicecode": "134",
"forecast": {
"month": {
"jan": [
{
"id": "1",
"price": "12",
"Product": "1086",
"Qty": "14"
},
{
"id": "2",
"price": "19",
"Product": "1746",
"Qty": "45"
}
],
"april": [
{
"id": "3",
"price": "89",
"Product": "1986",
"Qty": "15"
},
{
"id": "1",
"price": "12",
"Product": "1086",
"Qty": "145"
}
],
"jun": [
{
"id": "81",
"price": "132",
"Product": "17086",
"Qty": "1445"
},
{
"id": "11",
"price": "132",
"Product": "10786",
"Qty": "1445"
}
]
}
},
"message": "Competitor Sales."
}
what i did is i took all the response separately with the key and stored in MAp, now what i want to do is to display the array in according to month in View pager. 我所做的是我将所有响应分别与键分开保存在MAp中,现在要做的是在View pager中根据月份显示数组。 so tell me map'll do good or any alternative....
所以告诉我map'll做得好或任何其他选择。
If you put ArrayMap correctly, you could get it as below: 如果正确放置ArrayMap,则可以如下所示:
Model model = map.get("april");
String id = model.id;
String price = model.price;
.....
You should ensure the key
variable in your codes is a month name. 您应该确保代码中的
key
变量是月份名称。
Is the month the key of your map? 月是您地图的关键吗? If so, then you could simply define a list of months and retrieve the values accordingly, something like this:
如果是这样,那么您可以简单地定义一个月份列表并相应地检索值,如下所示:
for(String month: Arrays.asList("jan", "feb", "mar", "apr", "...")) {
if(map.containsKey(month)) { //skip months if not present
//--> code here
Model model = map.get(month);
}
}
You're definitely on the right idea with maps, since you're able to assign each month an object. 使用地图绝对是正确的主意,因为您可以每月分配一个对象。
However, you may want to consider using a LinkedHashMap
instead of an ArrayMap
because a LinkedHashMap
preserves the insertion order, while ArrayMap's docs does not mention preserving insertion order anywhere. 但是,您可能要考虑使用
LinkedHashMap
而不是ArrayMap
因为LinkedHashMap
保留插入顺序,而ArrayMap的文档没有提到在任何地方都保留插入顺序。
The reason preserving insertion is important is because the months
object in the JSON are presented in the order that you want to display it in. Therefore, if you parse "January" first, it will be guaranteed to be at the front of the Map. 保留插入之所以重要很重要,是因为JSON中的
months
对象是按照您想要显示的顺序显示的。因此,如果首先解析“ January”,则可以保证它位于Map的前面。
You can declare and initialize a LinkedHashMap as follows: 您可以按以下方式声明和初始化LinkedHashMap:
Map<String, ArrayList<Model>> map = new LinkedHashMap<>();
I am assuming that you have correctly parsed this json into your objects, since you did not say anything in regards to the map being incorrectly populated, and by the placement of your arrows "<-----" 我假设您已正确将此json解析为您的对象,因为您未对未正确填充地图以及通过放置箭头“ <-----”说什么
Now, for the code to actually use the Map. 现在,让代码实际使用Map。
for (String monthName : map.keySet()) {
ArrayList<Model> modelArray = map.get(monthName);
//now do whatever your view pager needs to do with this Model and its fields
for (Model model : modelArray) {
String price = model.getPrice(); //if you have a getter
String product = model.product; //if the field is public
}
}
Notes: 笔记:
Using the LinkedHashMap's keyset is valid because the documentation guarantees that the keyset will be in insertion order. 使用LinkedHashMap的密钥集是有效的,因为文档保证密钥集将按插入顺序进行。 Also, this SO answer also confirms this fact.
同样, 这个SO答案也证实了这一事实。
I personally recommend GSON for parsing JSON objects, but since you seem to be okay with the parsing, this is not a big deal. 我个人建议使用GSON解析JSON对象,但是由于您似乎对解析没有问题,所以这没什么大不了的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.