我有一个对象数组列表(list1),它包含座位号、图像及其 ID。我如何遍历 list1 并将每个数据存储到另一个列表中。前提是seat_no。匹配另一个固定大小为 50 的列表 (list2) 的索引。我应该创建另一个对象类吗?请帮助...
例子:
List1 包含
|seat_no | id | image |
======================================
| 1 | 001 | R.drawable.chair |
| 2 | 002 | R.drawable.chair |
| 3 | 003 | R.drawable.chair |
| 4 | 004 | R.drawable.chair |
| 7 | 005 | R.drawable.chair |
| 10 | 006 | R.drawable.chair |
List2 应包含
|index | id | seat_no |
=============================
| 1 | 001 | 1 |
| 2 | 002 | 2 |
| 3 | 003 | 3 |
| 4 | 004 | 4 |
| 5 |"blank" |"blank" |
| 6 |"blank" |"blank" |
| 7 | 005 | 7 |
| 8 |"blank" |"blank" |
| 9 |"blank" |"blank" |
| 10 | 006 | 10 |
这是我的代码
main.java
ArrayList<HashMap<String, String>> take = new ArrayList<HashMap<String, String>>();
try
{
JSONObject jsonArray = new JSONObject(result);
JSONArray info = jsonArray.getJSONArray("info");
list1 = new ArrayList<ViewGridObject>();
for (int i = 0; i < info.length(); i++)
{
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = info.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("sid", e.getString("id"));
map.put("flag", Integer.toString(seat[0]));
map.put("seat_no", e.getString("seat_no"));
take.add(map);
list1.add(new ViewGridObject(e.getString("id"), R.drawable.chair, e.getString("seat_no")));
}
ViewGridObject.java
public class ViewGridObject
{
public String stud_id, seat_no;
int chair;
public ViewGridObject(String stud_id, int chair, String seat_no)
{
this.stud_id = stud_id;
this.chair = chair;
this.seat_no = seat_no;
}
}