0

In my android application I'm receiving a JSONArray and I'm parsing it and storing it in a arraylist and appending that to listview but I'm facing an error entire list is showing in a single list item.but I'm receiving 4 items.This is all what I did.

    AsyncHttpClient client = new AsyncHttpClient();
    RequestParams params = new RequestParams();
    params.put("topicsJSON",composeJSON());
          client.post("http://www.example.com/load_topics.php",params,newAsyncHttpResponseHandler()
    {

        public void onSuccess(String response)
        {


            Gson gson = new GsonBuilder().create();
            try 
            {

                JSONArray arr = new JSONArray(response);

                for (int i = 0; i < arr.length(); i++) 
                {
                    //JSONObject obj = (JSONObject) arr.get(i);

                    list.add(arr.get(i).toString());
                    load_data();


                }
            }
            catch (JSONException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

                }

        @Override
        public void onFailure(int statusCode, Throwable error,String content)

        {

            if (statusCode == 404) 

            {
                Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
            } 

            else if (statusCode == 500) 

            {
                Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
            } 

            else 

            {
                Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet]",
                        Toast.LENGTH_LONG).show();
            }
        }

    });



}

private String composeJSON() {
    // TODO Auto-generated method stub
    ArrayList<HashMap<String, String>> check_in_List;
    check_in_List = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> map = new HashMap<String, String>();

    map.put("subject_code",received_subject_code);


    check_in_List.add(map);
    Gson gson = new GsonBuilder().create();
    return gson.toJson(check_in_List);

}

public void load_data()
{
    ArrayAdapter<String> phy = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);

    l1.setAdapter(phy);

    l1.setOnItemClickListener(new OnItemClickListener() { 
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
            // When clicked, show a toast with the TextView text 

            String pos = (String) l1.getItemAtPosition(position);

    }); 

} 
1

4 Answers 4

1

I'm facing an error entire list is showing in a single list item.but I'm receiving 4 items

Because calling load_data(); inside for-loop which is used for getting data from JSONArray in ArrayList

Move load_data(); outside for-loop to show each item in separate row in ListView

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

Comments

0

don't every time add list item, create adapter,

set adapter one time with empty list.

after end parse Jsonarray and add data, adapter.notifyDataSetChanged();

Comments

0

Replace this:

            for (int i = 0; i < arr.length(); i++) 
            {
                //JSONObject obj = (JSONObject) arr.get(i);

                list.add(arr.get(i).toString());
                load_data();


            }

Via

    for (int i = 0; i < arr.length(); i++) 
   {
           //JSONObject obj = (JSONObject) arr.get(i);

           list.add(arr.get(i).toString());
    }
load_data();

2 Comments

Hope this may help you!
this is not working bro,its showing 4 list items but its showing like 1st item,1st item 2 item,1st item 2 item,3 item and soo on...
0

Follow this:

private List<String> list = new ArrayList<String>();

try{
    JSONArray arr = json.getJSONArray("JSONArray Name");
        for (int i = 0; i < arr.length(); i++) {
            JSONObject c = arr.getJSONObject(i);
            list.add(c.getString("Object Name"));
        }
}catch (JSONException e) {
    // TODO Auto-generated catch block
        e.printStackTrace();
}

load_data();

3 Comments

Ok so make sure that ur list is full with data by print the size of list in Log and if it not working then you have to give the code of load_data().
it is returning the list size as 4 but its returning as JSON data in the listview
Can you give a screenshot?

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.