Android Open Source - Android-JSON-Parse-Images-and-Texts-Tutorial Main Activity






From Project

Back to project page Android-JSON-Parse-Images-and-Texts-Tutorial.

License

The source code is released under:

Apache License

If you think the Android project Android-JSON-Parse-Images-and-Texts-Tutorial listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.androidbegin.jsonparsetutorial;
//from  w  ww .  j a va  2 s .  c o m
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

public class MainActivity extends Activity {
  // Declare Variables
  JSONObject jsonobject;
  JSONArray jsonarray;
  ListView listview;
  ListViewAdapter adapter;
  ProgressDialog mProgressDialog;
  ArrayList<HashMap<String, String>> arraylist;
  static String RANK = "rank";
  static String COUNTRY = "country";
  static String POPULATION = "population";
  static String FLAG = "flag";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from listview_main.xml
    setContentView(R.layout.listview_main);
    // Execute DownloadJSON AsyncTask
    new DownloadJSON().execute();
  }

  // DownloadJSON AsyncTask
  private class DownloadJSON extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
      super.onPreExecute();
      // Create a progressdialog
      mProgressDialog = new ProgressDialog(MainActivity.this);
      // Set progressdialog title
      mProgressDialog.setTitle("Android JSON Parse Tutorial");
      // Set progressdialog message
      mProgressDialog.setMessage("Loading...");
      mProgressDialog.setIndeterminate(false);
      // Show progressdialog
      mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
      // Create the array 
      arraylist = new ArrayList<HashMap<String, String>>();
      // Retrive JSON Objects from the given website URL in JSONfunctions.class
      jsonobject = JSONfunctions
          .getJSONfromURL("http://www.androidbegin.com/tutorial/jsonparsetutorial.txt");

      try {
        // Locate the array name
        jsonarray = jsonobject.getJSONArray("worldpopulation");

        for (int i = 0; i < jsonarray.length(); i++) {
          HashMap<String, String> map = new HashMap<String, String>();
          jsonobject = jsonarray.getJSONObject(i);
          // Retrive JSON Objects
          map.put("rank", jsonobject.getString("rank"));
          map.put("country", jsonobject.getString("country"));
          map.put("population", jsonobject.getString("population"));
          map.put("flag", jsonobject.getString("flag"));
          // Set the JSON Objects into the array
          arraylist.add(map);
        }
      } catch (JSONException e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
      }
      return null;
    }

    @Override
    protected void onPostExecute(Void args) {
      // Locate the listview in listview_main.xml
      listview = (ListView) findViewById(R.id.listview);
      // Pass the results into ListViewAdapter.java
      adapter = new ListViewAdapter(MainActivity.this, arraylist);
      // Binds the Adapter to the ListView
      listview.setAdapter(adapter);
      // Close the progressdialog
      mProgressDialog.dismiss();
    }
  }
}




Java Source Code List

com.androidbegin.jsonparsetutorial.FileCache.java
com.androidbegin.jsonparsetutorial.ImageLoader.java
com.androidbegin.jsonparsetutorial.JSONfunctions.java
com.androidbegin.jsonparsetutorial.ListViewAdapter.java
com.androidbegin.jsonparsetutorial.MainActivity.java
com.androidbegin.jsonparsetutorial.MemoryCache.java
com.androidbegin.jsonparsetutorial.SingleItemView.java
com.androidbegin.jsonparsetutorial.Utils.java