java - 如何根据列表大小将数据存储到另一个ArrayList中?
问题描述
我有下面的for循环
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString("cat_id");
String title = c.getString("cat_name");
String image = c.getString("cat_img");
mData.add(id);
mData.add(title);
mData.add(image);
categoryHashMap.put(title, mData);
mData.clear();
}
showLog("index: "+ categoryHashMap.size());
if (jsonDataListener != null) {
// jsonDataListener.onClickData(tempList);
}
mData 是 ArrayList。现在contacts.length是7。但是在完成for循环后mData大小显示为21。这是正确的,因为显然有3个字段,即id、title和image。但我想要的是我想将该数据存储到另一个数组列表中,但是新的数组列表大小应该是 7,并且通过扩展每个索引/sie,应该有 3 个字段。所以,我认为它会是这样的:
private List<String> mData;
private List<List<String>> tempList;
但问题是尺寸显示为0!
我也尝试使用 hashmap ( private Map<String, List<String>> categoryHashMap = new HashMap<>();
),第一个参数是字符串,第二个是列表,但它没有成功。
输出应该是:在新的arraylist中
索引 0 -> id1 title1 image1
索引 1 -> id2 title2 image2
……
编辑:完整代码:
private List<String> mData;
private List<List<String>> tempList;
private Map<String, List<String>> categoryHashMap = new HashMap<>();
private void getTextFromJSON(String jsonParsingString) {
if (jsonParsingString != null) {
try {
JSONObject jsonObj = new JSONObject(jsonParsingString);
// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("data");
// looping through All data
mData.clear();
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString("cat_id");
String title = c.getString("cat_name");
String image = c.getString("cat_img");
mData.add(id);
mData.add(title);
mData.add(image);
/*for (int j=i; j<i+3; j++ ) {
categoryHashMap.put(title, mData);
}*/
tempList.add(mData);
}
showLog("index: "+ tempList);
if (jsonDataListener != null) {
// jsonDataListener.onClickData(tempList);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(mContext.getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
if (jsonDataListener != null) {
jsonDataListener.onJSONParsingError(e.getMessage());
}
}
} else {
Log.e(TAG, "Couldn't get JSON from server.");
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(mContext.getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
if (jsonDataListener != null) {
jsonDataListener.onJSONParsingError("Couldn't get json from server. Check LogCat for possible errors!");
}
}
}
解决方案
试试下面的实现
List<String> mData;
List<List<String>> tempList = new ArrayList<>();
for (int i = 0; i < contacts.length(); i++) {
mData = new ArrayList<>();
JSONObject c = contacts.getJSONObject(i);
String id = c.getString("cat_id");
String title = c.getString("cat_name");
String image = c.getString("cat_img");
mData.add(id);
mData.add(title);
mData.add(image);
tempList.add(mData);
}
现在tempList
大小为 7,每个包含 3 个项目
推荐阅读
- ssh - 如何在运行 ansible-playbook 时发送公钥?
- javascript - React 将键盘事件导航添加到网站
- html - 如何仅在底部向曲线添加描边?
- sql-server-2008-r2 - sql server 在逗号后按顺序拆分字符串
- python-3.x - 从 pandas 数据框生成 html
- javascript - 使用“nofollow/赞助”时,window.open 在新窗口而不是新选项卡中打开
- python - 我们如何在 pandas 中合并 csv 文件?
- html - 在托管程序中上传的文件转到 //
- typo3 - TYPO3 10 通过扩展模型从前端上传多张图片
- correlation - 如何从使用 GGally 完成的相关矩阵中删除“Corr”文本?