首页 > 解决方案 > 如何根据列表大小将数据存储到另一个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!");
            }


        }
    }

标签: javaandroid

解决方案


试试下面的实现

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 个项目

在此处输入图像描述


推荐阅读