Advertisement
Guest User

Untitled

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