2

I have the following response, and I want to display all eqid's in invidual TextView's, but I don't know how to achive this, below is my snippet code and response to get data from the server and display in textview.
Can anyone help me with this?

{
  "earthquakes":[
    {
      "eqid":"c0001xgp",

    },
    {
      "eqid":"c000905e",

    },
    {
      "eqid":"2007hear",

    },
    {
      "eqid":"c00090da",

    },
    {
      "eqid":"2007aqbk",

    },
    {
      "eqid":"2007hec6",

    },
    {
      "eqid":"a00043nx",

    },
    {
      "eqid":"2010utc5",

    },

  ]
}

Code

   ServiceHandler sh = new ServiceHandler();
    jsonStr = sh.makeServiceCall(USER_URL, ServiceHandler.GET);
    Log.d("Response: ", "> " + jsonStr);

    try{
        JSONObject json = new JSONObject(jsonStr);
        JSONArray items = json.getJSONArray("product");

        parent_layout = (RelativeLayout)findViewById(R.id.rl);
        for(int i =0; i<items.length(); i++){



            TextView textView = new TextView(this);
            textView.setText(items.getJSONObject(i).getString(USER_NAME));

            parent_layout.addView(textView);
        }

2 Answers 2

1

try{
    JSONObject json = new JSONObject(jsonstr);
    JSONArray items = json.getJSONArray("earthquakes");

    for(int i =0; i<items.length(); i++){
        TextView t=new TextView(context);
        t.setText(items.getJSONObject(i).getString('eqid'));
        ParentLayout.addView(t);
    }
}catch(JSONException e){
    e.printStacktrace();
}

Sign up to request clarification or add additional context in comments.

2 Comments

see my edited code in question..i dont have any error..but response is not displaying
as you have used Relative layout your textview will be overlapping on each other...Try Linear layout or add attributes like layoutToLeftOf or above or below and like that....
1

You have a single JSON object which contains a Single JSON array. So you first retrieve the array from the object and then you are able to loop over it to retrieve all the items. In below code I assume you have the destined TextView objects in an array.

try{
    JSONObject json = new JSONObject(jsonstr);
    JSONArray items = json.getJSONArray("earthquakes");

    for(int i =0; i<items.length(); i++){
        textViews[i].setText(items.getJSONObject(i).getString('eqid'));
    }
}catch(JSONException e){
    e.printStacktrace();
}

7 Comments

can you tell one things??
stackoverflow.com/questions/18917214/… this question they chagne color..can i add buttons alternatvly
That would be a separate question. Questions should stay on topic.
array type expected found android widget textview..it shows
@Roman In my answer I state that I assume that the destined TextView objects are in an array and can therefore be referenced like textViews[i] in the loop. If the textView variable does not reference a TextView array you will get this error. I assume you know yourself where to find the TextViews you want to put the data.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.