简体   繁体   中英

Viewing parse Json Data in listview

I have the code to parse json data from postgresql in android. It works fine. Now I want to view it using listview. I have converted the data from postgis to json and parsed it in android. How will i view this using list view ?

The following is my code.

public class MainActivity extends Activity {
    private ProgressDialog pDialog;
    String result;
    StringBuilder sb;
    InputStream is;
    JSONArray jArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new task().execute();
    }

    class task extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            Log.i("doing", "back");
            ArrayList<NameValuePair> nameValuePairs = 
            new ArrayList<NameValuePair>();
            // http post

            try {
                HttpClient httpclient = new DefaultHttpClient();


                HttpPost httppost = new HttpPost(
                        "http://XXXXXXXXXX/abcd.php");

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();

            } catch (Exception e) {
                Log.e("log_tag", "Error in http connection" + e.toString());
            }

            // convert response to string
            try {

                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(is, "iso-8859-1"), 8);
                sb = new StringBuilder();
                sb.append(reader.readLine() + "\n");
                String line = "0";
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }

                is.close();
                result = sb.toString();
                Log.i("Result", "Json" + result);
            } catch (Exception e) {
                Log.e("log_tag", "Error converting result " + e.toString());
            }

            String name;
            try {

                jArray = new JSONArray(result);

                JSONObject json_data = null;
                for (int i = 0; i < jArray.length(); i++) {
                    json_data = jArray.getJSONObject(i);

                    String gid = json_data.getString("gid");
                    String district = json_data.getString("district");
                    Log.i("LOG", "resultttttttt" + gid + " " + district);

                }

            } catch (JSONException e1) {
                Toast.makeText(getBaseContext(), "No Data Found",
                        Toast.LENGTH_LONG).show();
            } catch (ParseException e1) {
                e1.printStackTrace();
            } catch (NullPointerException e) {
                // TODO: handle exception
                Toast.makeText(getBaseContext(), "No Data Found",
                        Toast.LENGTH_LONG).show();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            if (pDialog.isShowing())
                pDialog.dismiss();
        }
    }
}

You have to make an arrayliat and a Custom adapter to fill array Item in ListView.

  1. Get data from json to Arraylist.
  2. Make customAdapter and Inflate custom Item design in your listview.
  3. pass arraylist to Custom Adapter.
  4. set Adapter to the ListView.

For complete demo you can use ThisLink

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM