acano

Untitled

Mar 16th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.41 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3.  
  4. import org.json.JSONArray;
  5. import org.json.JSONException;
  6. import org.json.JSONObject;
  7.  
  8. import android.app.ListActivity;
  9. import android.app.ProgressDialog;
  10. import android.content.Intent;
  11. import android.os.AsyncTask;
  12. import android.os.Bundle;
  13. import android.util.Log;
  14. import android.view.View;
  15. import android.widget.AdapterView;
  16. import android.widget.AdapterView.OnItemClickListener;
  17. import android.widget.ListAdapter;
  18. import android.widget.ListView;
  19. import android.widget.SimpleAdapter;
  20. import android.widget.TextView;
  21.  
  22. public class MainActivity extends ListActivity {
  23.  
  24. private ProgressDialog pDialog;
  25.  
  26. // URL to get contacts JSON
  27. private static String url = "http://api.androidhive.info/contacts/";
  28.  
  29. // JSON Node names
  30. private static final String TAG_CONTACTS = "contacts";
  31. private static final String TAG_ID = "id";
  32. private static final String TAG_NAME = "name";
  33. private static final String TAG_EMAIL = "email";
  34. private static final String TAG_ADDRESS = "address";
  35. private static final String TAG_GENDER = "gender";
  36. private static final String TAG_PHONE = "phone";
  37. private static final String TAG_PHONE_MOBILE = "mobile";
  38. private static final String TAG_PHONE_HOME = "home";
  39. private static final String TAG_PHONE_OFFICE = "office";
  40.  
  41. // contacts JSONArray
  42. JSONArray contacts = null;
  43.  
  44. // Hashmap for ListView
  45. ArrayList<HashMap<String, String>> contactList;
  46.  
  47. @Override
  48. public void onCreate(Bundle savedInstanceState) {
  49. super.onCreate(savedInstanceState);
  50. setContentView(R.layout.activity_main);
  51.  
  52. contactList = new ArrayList<HashMap<String, String>>();
  53.  
  54. ListView lv = getListView();
  55.  
  56. // Listview on item click listener
  57. lv.setOnItemClickListener(new OnItemClickListener() {
  58.  
  59. @Override
  60. public void onItemClick(AdapterView<?> parent, View view,
  61. int position, long id) {
  62. // getting values from selected ListItem
  63. String name = ((TextView) view.findViewById(R.id.name))
  64. .getText().toString();
  65. String cost = ((TextView) view.findViewById(R.id.email))
  66. .getText().toString();
  67. String description = ((TextView) view.findViewById(R.id.mobile))
  68. .getText().toString();
  69.  
  70. // Starting single contact activity
  71. Intent in = new Intent(getApplicationContext(),
  72. SingleContactActivity.class);
  73. in.putExtra(TAG_NAME, name);
  74. in.putExtra(TAG_EMAIL, cost);
  75. in.putExtra(TAG_PHONE_MOBILE, description);
  76. startActivity(in);
  77.  
  78. }
  79. });
  80.  
  81. // Calling async task to get json
  82. new GetContacts().execute();
  83. }
  84.  
  85. /**
  86. * Async task class to get json by making HTTP call
  87. * */
  88. private class GetContacts extends AsyncTask<Void, Void, Void> {
  89.  
  90. @Override
  91. protected void onPreExecute() {
  92. super.onPreExecute();
  93. // Showing progress dialog
  94. pDialog = new ProgressDialog(MainActivity.this);
  95. pDialog.setMessage("Please wait...");
  96. pDialog.setCancelable(false);
  97. pDialog.show();
  98.  
  99. }
  100.  
  101. @Override
  102. protected Void doInBackground(Void... arg0) {
  103. // Creating service handler class instance
  104. ServiceHandler sh = new ServiceHandler();
  105.  
  106. // Making a request to url and getting response
  107. String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
  108.  
  109. Log.d("Response: ", "> " + jsonStr);
  110.  
  111. if (jsonStr != null) {
  112. try {
  113. JSONObject jsonObj = new JSONObject(jsonStr);
  114.  
  115. // Getting JSON Array node
  116. contacts = jsonObj.getJSONArray(TAG_CONTACTS);
  117.  
  118. // looping through All Contacts
  119. for (int i = 0; i < contacts.length(); i++) {
  120. JSONObject c = contacts.getJSONObject(i);
  121.  
  122. String id = c.getString(TAG_ID);
  123. String name = c.getString(TAG_NAME);
  124. String email = c.getString(TAG_EMAIL);
  125. String address = c.getString(TAG_ADDRESS);
  126. String gender = c.getString(TAG_GENDER);
  127.  
  128. // Phone node is JSON Object
  129. JSONObject phone = c.getJSONObject(TAG_PHONE);
  130. String mobile = phone.getString(TAG_PHONE_MOBILE);
  131. String home = phone.getString(TAG_PHONE_HOME);
  132. String office = phone.getString(TAG_PHONE_OFFICE);
  133.  
  134. // tmp hashmap for single contact
  135. HashMap<String, String> contact = new HashMap<String, String>();
  136.  
  137. // adding each child node to HashMap key => value
  138. contact.put(TAG_ID, id);
  139. contact.put(TAG_NAME, name);
  140. contact.put(TAG_EMAIL, email);
  141. contact.put(TAG_PHONE_MOBILE, mobile);
  142.  
  143. // adding contact to contact list
  144. contactList.add(contact);
  145. }
  146. } catch (JSONException e) {
  147. e.printStackTrace();
  148. }
  149. } else {
  150. Log.e("ServiceHandler", "Couldn't get any data from the url");
  151. }
  152.  
  153. return null;
  154. }
  155.  
  156. @Override
  157. protected void onPostExecute(Void result) {
  158. super.onPostExecute(result);
  159. // Dismiss the progress dialog
  160. if (pDialog.isShowing())
  161. pDialog.dismiss();
  162. /**
  163. * Updating parsed JSON data into ListView
  164. * */
  165. ListAdapter adapter = new SimpleAdapter(
  166. MainActivity.this, contactList,
  167. R.layout.list_item, new String[] { TAG_NAME, TAG_EMAIL,
  168. TAG_PHONE_MOBILE }, new int[] { R.id.name,
  169. R.id.email, R.id.mobile });
  170.  
  171. setListAdapter(adapter);
  172. }
  173.  
  174. }
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment