Advertisement
Guest User

Untitled

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