java - 将 JSON 条目添加到数组中以在 ListView 中使用

标签 java android

我有以下代码获取选择条目出现的次数:

int k = 0;
             int j = 0;
             try {
                jsonArray = new JSONArray(result);
                for(int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObj = (JSONObject)jsonArray.get(i); // get the josn object
                    if(jsonObj.getString("type").equals("image")) { // compare for the key-value
                        k++;
                    }
                    if(jsonObj.getString("type").equals("text")) { // compare for the key-value
                        j++;
                    }
                }
                Toast.makeText(getApplicationContext(), String.valueOf(k), 2000).show();
                Toast.makeText(getApplicationContext(), String.valueOf(j), 2000).show();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

我的 JSON 文件是:

[
    {
        "id": "12",
        "type": "text",
        "data": "This is a test"
    },
    {
        "id": "5465465",
        "type": "image",
        "data": "http://pagesbyz.com/theImages/gallery/portfolio_app07.png"
    },
    {
        "id": "982",
        "type": "text",
        "data": "THIS IS A TEST TEXT"
    },
    {
        "id": "5500",
        "type": "text",
        "data": "http://pagesbyz.com/theImages/gallery/portfolio_app03.png"
    }
]

所以... k = 1j = 3

如何获取匹配条目的整个数组?

例如,一旦类型等于 image,我想设置一个数组来保存信息。

如何用 Java 翻译以下内容?

for (as many times an "image" entry appears) {
    if ( type == "image") {
         imgarr[index found][id] = "5465465";
         imgarr[index found][type] = "image";
         imgarr[index found][data] = "http://pagesbyz.com/theImages/gallery/portfolio_app07.png";
    }
}

for (as many times an "text" entry appears) {
    if ( type == "text") {
         txtarr[index found][id] = "12";
         txtarr[index found][type] = "text";
         txtarr[index found][data] = "This is a test";

        txtarr[index found][id] = "082";
        txtarr[index found][id] = "text";
        txtarr[index found][id] = "THIS IS A TEST TEXT";

        and so forth...
    }
}

对于等于“文本”的类型也是如此吗?

最佳答案

创建 2 个新的 jsonArray 变量,一个用于图像,一个用于类型,每当类型为图像时,将对象添加到 jsonimage 中,同样用于 jsontext

       try {
            JSONArray jsonimage = new JSONArray();
            JSONArray jsontext = new JSONArray();
            jsonArray = new JSONArray(result);
            for(int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObj = (JSONObject)jsonArray.get(i); // get the josn object
                if(jsonObj.getString("type").equals("image")) { // compare for the key-value

                    jsonimage.put(jsonObj);
                    k++;

                }
                if(jsonObj.getString("type").equals("text")) { // compare for the key-value
                    jsontext.put(jsonObj);
                    j++;
                }
            }
            Toast.makeText(getApplicationContext(), String.valueOf(k), 2000).show();
            Toast.makeText(getApplicationContext(), String.valueOf(j), 2000).show();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

希望这就是您正在寻找的或给您提示。

编辑:

要将 jsonArray 添加到 ListView ,您可以创建一个 hashMap 或简单地为 JSONArray 中的每个条目创建字符串字符串。

       String sId = new String[jsonimage.length()];
        String sType = new String[jsonimage.length()];
        String sData = new String[jsonimage.length()];
for (int i = 0 ; i < jsonimage.length(); i++)
        {
            JSONObject c = jsonimage.getJSONObject(i);

            String id = c.getString("id");
            String type = c.getString("type");
            String data = c.getString("data");



            sId[i] = id;
            sType[i] = type;
            sData[i] = data;                

        }

然后在从 JSONArray 中获取数据后...

有一个包含 3 个 TextView 的自定义 View 并添加它

listView.setAdapter(new CustomAdapter(yourClass.this));

然后简单地将生成的 String[position] 添加到 CustomAdapter 的 getView 中

关于java - 将 JSON 条目添加到数组中以在 ListView 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19872000/

相关文章:

android - 如何在 Android DatePicker 中设置最小和最大日期?

android - 如何删除文件资源管理器sdcard中的文件夹?

java - 我无法将基于教程的项目部署到 Google 应用程序引擎

java - 如何在特定情况下运行特定的 JUnit 测试集?

java - 在 Java 泛型中共享通配符

java - 有没有办法更改首选项标题内图标的颜色?

android - 从 adb 命令获取设备信息(如产品、型号)

android - 如何在 Android 中显示另一个 Activity 中的一个 Activity ?

java - Java中接口(interface)和类的区别

java - 我的 do while 循环遇到问题