Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company.napp;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Gravity;
- import android.widget.AbsListView;
- import android.widget.ListView;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.ArrayList;
- import java.util.HashMap;
- public class MainActivity extends Activity {
- // Declare Variables
- ArrayList<HashMap<String, String>> arraylist1;
- int page=1;
- JSONObject jsonobject;
- JSONArray jsonarray;
- ListView listview;
- ListViewAdapter adapter;
- ProgressDialog mProgressDialog;
- ArrayList<HashMap<String, String>> arraylist;
- static String TITLE = "title";
- static String ADDRESS = "address";
- static String COUNTRY = "country";
- static String IMAGE = "image";
- static String SERVICETYPE="serviceType";
- static String PHNUM="phnum";
- static String DESCRIPTION="description";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.listview_main);
- // Execute DownloadJSON AsyncTask
- new DownloadJSON().execute();
- listview = (ListView) findViewById(R.id.listview);
- listview.setOnScrollListener(new AbsListView.OnScrollListener() {
- @Override
- public void onScrollStateChanged(AbsListView view, int scrollState) {
- // TODO Auto-generated method stub
- int threshold = 1;
- int count = listview.getCount();
- System.out.println("nLast visible = "+listview.getLastVisiblePosition()+"nCount = "+count);
- if (scrollState == SCROLL_STATE_IDLE) {
- if (listview.getLastVisiblePosition()+1 > count-1) {
- // Execute LoadMoreDataTask AsyncTask
- new LoadMoreDataTask().execute();
- }
- }
- }
- @Override
- public void onScroll(AbsListView view, int firstVisibleItem,
- int visibleItemCount, int totalItemCount) {
- // TODO Auto-generated method stub
- }
- });
- }
- //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("Napp");
- // Set progressdialog message
- mProgressDialog.setMessage("Loading...");
- mProgressDialog.setIndeterminate(false);
- // Show progressdialog
- mProgressDialog.show();
- mProgressDialog.setCanceledOnTouchOutside(false);
- //mProgressDialog.getWindow().setGravity(Gravity.BOTTOM);
- }
- @Override
- protected Void doInBackground(Void... params) {
- // Create an array
- arraylist = new ArrayList<HashMap<String, String>>();
- // Retrieve JSON Objects from the given URL address
- jsonobject = JSONfunctions.getJSONfromURL("http://54.169.88.65/events/TA/JsonTA.php?page=1");
- try {
- // Locate the array name in JSON
- jsonarray = jsonobject.getJSONArray("Contacts");
- for (int i = 0; i < jsonarray.length(); i++) {
- HashMap<String, String> map = new HashMap<String, String>();
- jsonobject = jsonarray.getJSONObject(i);
- // Retrive JSON Objects
- String t, a, d, s;
- t = jsonobject.getString("title");
- t = t.replaceAll("\n", "");
- //t="n"+t;
- a = jsonobject.getString("address");
- a = a.replaceAll("\n", "");
- d = jsonobject.getString("description");
- d = d.replaceAll("\n", "");
- s = jsonobject.getString("serviceType");
- s = s.replaceAll("\n", "");
- //s=s+"n";
- map.put("title", t);
- map.put("address", a);
- map.put("country", jsonobject.getString("country"));
- map.put("image", jsonobject.getString("image"));
- map.put("serviceType", s);
- map.put("phnum", jsonobject.getString("phnum"));
- map.put("description", d);
- // 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);
- // Set the adapter to the ListView
- listview.setAdapter(adapter);
- // Close the progressdialog
- mProgressDialog.dismiss();
- }
- }
- private class LoadMoreDataTask extends AsyncTask <Void, Void, Void> {
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- // Create a progressdialog
- mProgressDialog = new ProgressDialog(MainActivity.this);
- // Set progressdialog title
- //mProgressDialog.setTitle("Napp");
- // Set progressdialog message
- mProgressDialog.setMessage("Loading...");
- mProgressDialog.setIndeterminate(false);
- //Show progressdialog
- mProgressDialog.show();
- mProgressDialog.setCanceledOnTouchOutside(false);
- //mProgressDialog.getWindow().setGravity(Gravity.BOTTOM);
- }
- @Override
- protected Void doInBackground(Void... params) {
- // Create an array
- page+=1;
- //arraylist = new ArrayList<HashMap<String, String>>();
- // Retrieve JSON Objects from the given URL address
- jsonobject = JSONfunctions.getJSONfromURL("http://54.169.88.65/events/TA/JsonTA.php?page="+page);
- try {
- // Locate the array name in JSON
- jsonarray = jsonobject.getJSONArray("Contacts");
- arraylist1 = new ArrayList<HashMap<String, String>>();
- for (int i = 0; i < jsonarray.length(); i++) {
- HashMap<String, String> map = new HashMap<String, String>();
- jsonobject = jsonarray.getJSONObject(i);
- // Retrive JSON Objects
- String t,a,d,s;
- t=jsonobject.getString("title");
- t=t.replaceAll("\n", "");
- //t="n"+t;
- a=jsonobject.getString("address");
- a=a.replaceAll("\n","");
- d=jsonobject.getString("description");
- d=d.replaceAll("\n","");
- s=jsonobject.getString("serviceType");
- s=s.replaceAll("\n","");
- //s=s+"n";
- map.put("title",t);
- map.put("address",a);
- map.put("country", jsonobject.getString("country"));
- map.put("image", jsonobject.getString("image"));
- map.put("serviceType",s);
- map.put("phnum",jsonobject.getString("phnum"));
- map.put("description",d);
- // Set the JSON Objects into the array
- arraylist1.add(map);
- }
- } catch (JSONException e) {
- Log.e("Error", e.getMessage());
- e.printStackTrace();
- }
- return null;
- }
- @Override
- protected void onPostExecute(Void args) {
- // Set the JSON Objects into the array
- arraylist.addAll(arraylist1);
- //locate listview last item
- int position =listview.getLastVisiblePosition();
- adapter = new ListViewAdapter(MainActivity.this, arraylist);
- // Set the adapter to the ListView
- listview.setAdapter(adapter);
- //show the lastest retrieved results on the top
- listview.setSelectionFromTop(position,0);
- adapter.notifyDataSetChanged();
- // Close the progressdialog
- mProgressDialog.dismiss();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement