Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将JSON条目添加到数组中以在ListView中使用_Java_Android - Fatal编程技术网

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

Java 将JSON条目添加到数组中以在ListView中使用,java,android,Java,Android,我有以下代码,它获取一个select条目出现的次数: int k = 0; int j = 0; try { jsonArray = new JSONArray(result); for(int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObj = (JSONObject)

我有以下代码,它获取一个select条目出现的次数:

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();
            }
所以<代码>k=1和
j=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...
    }
}

类型相同等于“text”?

创建两个新的jsonArray变量,一个用于image,一个用于type,只要类型是image,就将对象添加到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();
        }

只要在CustomAdapter的getView中添加生成的字符串[position],就可以创建两个新的jsonArray变量,一个用于image,一个用于type,只要类型是image,就可以将对象添加到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();
        }

只需在CustomAdapter的getView中添加生成的字符串[position]

,根据我的理解,您可以创建一个新的JSONArray,并且只要它匹配,就可以在循环完成时将条目添加到此数组中。所有匹配的条目都将在新的JSONArray中找到。是的,基本上是这样,我该如何实现呢?因为我想创建所找到数据的listview。据我所知,您可以创建一个新的JSONArray,只要它匹配,就可以在循环完成时将条目添加到此数组中。所有匹配的条目都将在新的JSONArray中找到。是的,基本上是这样,我该如何实现呢?因为我想创建找到的数据的列表视图。谢谢。现在,如果我想将数组添加到listview中,我将如何检索该数组?谢谢。现在,如果我想将数组添加到listview中,我将如何检索该数组?
       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;                

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