android - 解析JSONArray并在android中的ListView中显示数据

标签 android mysql json listview

在我的 Android 应用程序中,我收到一个 JSONArray,我正在解析它并将其存储在数组列表中并将其附加到 ListView ,但我遇到错误整个列表显示在单个列表项中。但是我'我收到了 4 件元素。这就是我所做的全部。

    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);

    }); 

} 

最佳答案

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

因为在for-loop内调用load_data();,用于从ArrayList中的JSONArray获取数据>

load_data(); 移至 for-loop 外部,以在 ListView 中的单独行中显示每个项目

关于android - 解析JSONArray并在android中的ListView中显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29816396/

相关文章:

javascript - 我如何使用 PHP 读取/解析 javascript 的对象列表?

安卓工作室 : java. lang.NoClassDefFoundError

android - 使用 Volley 实现带有 Recyclerview 的 JSON 对象

php - mysql pdo 中的一般错误

c# - 如何抑制 MySql.Data.MySqlClient.MySqlException 重复条目

ios - 不在表格 View 中快速显示数据

javascript - 通过在每个属性中找到的索引来排序 json 对象的最佳方法是什么?

android - RecyclerView 拖放 - 使用 ItemTouchHelper - 如何在拖动时更快地设置滚动速度?

Android LayoutParams 未正确更新

MySQL 连接,即使为 0