Reputation: 9225
I have the following code which gets the number how many times a select entry appears:
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();
}
My JSON file is:
[
{
"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"
}
]
So... k = 1
and j = 3
How can I get the entire array for the matched entry?
For example once the type equals image
I want an array set up to hold the information.
How do I translate the following in 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...
}
}
And the same for type equals "text"?
Upvotes: 1
Views: 122
Reputation: 7745
create 2 new jsonArray variables, one for image and one for type, whenever the type is image add the object to jsonimage and same for 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();
}
hope this is what you are looking for or give you a hint.
EDIT:
to add the jsonArray to the listview, you can either create a hashMap or simply create String of strings for each entry in the 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;
}
then after you having your data from the JSONArray...
have a custom view that holds 3 textview and add this
listView.setAdapter(new CustomAdapter(yourClass.this));
and simply add the generated String[position] in getView of the CustomAdapter
Upvotes: 1