Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.01 KB | None | 0 0
  1. package com.company.napp;
  2.  
  3. import android.app.Activity;
  4. import android.app.ProgressDialog;
  5. import android.os.AsyncTask;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.Gravity;
  9. import android.widget.AbsListView;
  10. import android.widget.ListView;
  11.  
  12. import org.json.JSONArray;
  13. import org.json.JSONException;
  14. import org.json.JSONObject;
  15.  
  16. import java.util.ArrayList;
  17. import java.util.HashMap;
  18.  
  19.  
  20. public class MainActivity extends Activity {
  21.  
  22. // Declare Variables
  23. ArrayList<HashMap<String, String>> arraylist1;
  24. int page=1;
  25. JSONObject jsonobject;
  26. JSONArray jsonarray;
  27. ListView listview;
  28. ListViewAdapter adapter;
  29. ProgressDialog mProgressDialog;
  30. ArrayList<HashMap<String, String>> arraylist;
  31. static String TITLE = "title";
  32. static String ADDRESS = "address";
  33. static String COUNTRY = "country";
  34. static String IMAGE = "image";
  35. static String SERVICETYPE="serviceType";
  36. static String PHNUM="phnum";
  37. static String DESCRIPTION="description";
  38.  
  39.  
  40. @Override
  41. protected void onCreate(Bundle savedInstanceState) {
  42.  
  43. super.onCreate(savedInstanceState);
  44. setContentView(R.layout.listview_main);
  45. // Execute DownloadJSON AsyncTask
  46. new DownloadJSON().execute();
  47.  
  48. listview = (ListView) findViewById(R.id.listview);
  49.  
  50. listview.setOnScrollListener(new AbsListView.OnScrollListener() {
  51.  
  52. @Override
  53. public void onScrollStateChanged(AbsListView view, int scrollState) {
  54. // TODO Auto-generated method stub
  55. int threshold = 1;
  56.  
  57. int count = listview.getCount();
  58. System.out.println("nLast visible = "+listview.getLastVisiblePosition()+"nCount = "+count);
  59. if (scrollState == SCROLL_STATE_IDLE) {
  60. if (listview.getLastVisiblePosition()+1 > count-1) {
  61. // Execute LoadMoreDataTask AsyncTask
  62.  
  63. new LoadMoreDataTask().execute();
  64.  
  65. }
  66. }
  67.  
  68. }
  69.  
  70. @Override
  71. public void onScroll(AbsListView view, int firstVisibleItem,
  72. int visibleItemCount, int totalItemCount) {
  73. // TODO Auto-generated method stub
  74.  
  75. }
  76.  
  77. });
  78.  
  79. }
  80.  
  81.  
  82. //DownloadJSON AsyncTask
  83.  
  84. private class DownloadJSON extends AsyncTask<Void, Void, Void> {
  85.  
  86. @Override
  87. protected void onPreExecute() {
  88. super.onPreExecute();
  89. // Create a progressdialog
  90. mProgressDialog = new ProgressDialog(MainActivity.this);
  91. // Set progressdialog title
  92. mProgressDialog.setTitle("Napp");
  93. // Set progressdialog message
  94. mProgressDialog.setMessage("Loading...");
  95. mProgressDialog.setIndeterminate(false);
  96. // Show progressdialog
  97. mProgressDialog.show();
  98. mProgressDialog.setCanceledOnTouchOutside(false);
  99. //mProgressDialog.getWindow().setGravity(Gravity.BOTTOM);
  100. }
  101.  
  102. @Override
  103. protected Void doInBackground(Void... params) {
  104. // Create an array
  105. arraylist = new ArrayList<HashMap<String, String>>();
  106. // Retrieve JSON Objects from the given URL address
  107. jsonobject = JSONfunctions.getJSONfromURL("http://54.169.88.65/events/TA/JsonTA.php?page=1");
  108.  
  109. try {
  110. // Locate the array name in JSON
  111. jsonarray = jsonobject.getJSONArray("Contacts");
  112.  
  113. for (int i = 0; i < jsonarray.length(); i++) {
  114. HashMap<String, String> map = new HashMap<String, String>();
  115. jsonobject = jsonarray.getJSONObject(i);
  116. // Retrive JSON Objects
  117. String t, a, d, s;
  118. t = jsonobject.getString("title");
  119. t = t.replaceAll("\n", "");
  120. //t="n"+t;
  121. a = jsonobject.getString("address");
  122. a = a.replaceAll("\n", "");
  123. d = jsonobject.getString("description");
  124. d = d.replaceAll("\n", "");
  125. s = jsonobject.getString("serviceType");
  126. s = s.replaceAll("\n", "");
  127. //s=s+"n";
  128.  
  129. map.put("title", t);
  130. map.put("address", a);
  131. map.put("country", jsonobject.getString("country"));
  132. map.put("image", jsonobject.getString("image"));
  133. map.put("serviceType", s);
  134. map.put("phnum", jsonobject.getString("phnum"));
  135. map.put("description", d);
  136. // Set the JSON Objects into the array
  137. arraylist.add(map);
  138. }
  139. } catch (JSONException e) {
  140. Log.e("Error", e.getMessage());
  141. e.printStackTrace();
  142. }
  143. return null;
  144.  
  145. }
  146.  
  147. @Override
  148. protected void onPostExecute(Void args) {
  149. // Locate the listview in listview_main.xml
  150. listview = (ListView) findViewById(R.id.listview);
  151. // Pass the results into ListViewAdapter.java
  152. adapter = new ListViewAdapter(MainActivity.this, arraylist);
  153. // Set the adapter to the ListView
  154. listview.setAdapter(adapter);
  155. // Close the progressdialog
  156. mProgressDialog.dismiss();
  157.  
  158.  
  159. }
  160. }
  161. private class LoadMoreDataTask extends AsyncTask <Void, Void, Void> {
  162.  
  163. @Override
  164. protected void onPreExecute() {
  165. super.onPreExecute();
  166. // Create a progressdialog
  167. mProgressDialog = new ProgressDialog(MainActivity.this);
  168. // Set progressdialog title
  169. //mProgressDialog.setTitle("Napp");
  170. // Set progressdialog message
  171. mProgressDialog.setMessage("Loading...");
  172. mProgressDialog.setIndeterminate(false);
  173. //Show progressdialog
  174. mProgressDialog.show();
  175. mProgressDialog.setCanceledOnTouchOutside(false);
  176. //mProgressDialog.getWindow().setGravity(Gravity.BOTTOM);
  177. }
  178.  
  179. @Override
  180. protected Void doInBackground(Void... params) {
  181. // Create an array
  182.  
  183. page+=1;
  184. //arraylist = new ArrayList<HashMap<String, String>>();
  185. // Retrieve JSON Objects from the given URL address
  186. jsonobject = JSONfunctions.getJSONfromURL("http://54.169.88.65/events/TA/JsonTA.php?page="+page);
  187.  
  188. try {
  189. // Locate the array name in JSON
  190. jsonarray = jsonobject.getJSONArray("Contacts");
  191. arraylist1 = new ArrayList<HashMap<String, String>>();
  192. for (int i = 0; i < jsonarray.length(); i++) {
  193. HashMap<String, String> map = new HashMap<String, String>();
  194. jsonobject = jsonarray.getJSONObject(i);
  195. // Retrive JSON Objects
  196. String t,a,d,s;
  197. t=jsonobject.getString("title");
  198. t=t.replaceAll("\n", "");
  199. //t="n"+t;
  200. a=jsonobject.getString("address");
  201. a=a.replaceAll("\n","");
  202. d=jsonobject.getString("description");
  203. d=d.replaceAll("\n","");
  204. s=jsonobject.getString("serviceType");
  205. s=s.replaceAll("\n","");
  206. //s=s+"n";
  207.  
  208. map.put("title",t);
  209. map.put("address",a);
  210. map.put("country", jsonobject.getString("country"));
  211. map.put("image", jsonobject.getString("image"));
  212. map.put("serviceType",s);
  213. map.put("phnum",jsonobject.getString("phnum"));
  214. map.put("description",d);
  215. // Set the JSON Objects into the array
  216. arraylist1.add(map);
  217.  
  218. }
  219. } catch (JSONException e) {
  220. Log.e("Error", e.getMessage());
  221. e.printStackTrace();
  222. }
  223. return null;
  224.  
  225. }
  226.  
  227. @Override
  228. protected void onPostExecute(Void args) {
  229.  
  230. // Set the JSON Objects into the array
  231. arraylist.addAll(arraylist1);
  232. //locate listview last item
  233. int position =listview.getLastVisiblePosition();
  234. adapter = new ListViewAdapter(MainActivity.this, arraylist);
  235.  
  236.  
  237. // Set the adapter to the ListView
  238. listview.setAdapter(adapter);
  239.  
  240. //show the lastest retrieved results on the top
  241. listview.setSelectionFromTop(position,0);
  242.  
  243.  
  244. adapter.notifyDataSetChanged();
  245.  
  246.  
  247. // Close the progressdialog
  248. mProgressDialog.dismiss();
  249.  
  250.  
  251.  
  252. }
  253.  
  254.  
  255.  
  256.  
  257.  
  258. }
  259.  
  260.  
  261.  
  262.  
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement