public void findProperties(String order, int page) { ringProgressDialog = ProgressDialog.show(this, "Please wait ...", "Finding Properties ...", true); ringProgressDialog.setCancelable(false); if (order.equalsIgnoreCase("")) { URL_FEED = "https://www.propertyfinder.ae/mobileapi?page=" + page; } else { URL_FEED = "https://www.propertyfinder.ae/mobileapi?page=" + page + "&order=" + order; } if (page == 0) { feedItems.clear(); } // making fresh volley request and getting json JsonObjectRequest jsonReq = new JsonObjectRequest( Request.Method.GET, URL_FEED, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { VolleyLog.d(TAG, "Response: " + response.toString()); if (response != null) { parseJsonFeed(response); ringProgressDialog.dismiss(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { VolleyLog.d(TAG, "Error: " + error.getMessage()); Toast.makeText(MainActivity.this, "Error Finding Properties!", Toast.LENGTH_SHORT) .show(); ringProgressDialog.dismiss(); } }); // Adding request to volley request queue AppController.getInstance().addToRequestQueue(jsonReq); }
@SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.propertyListView); paButton = (Button) findViewById(R.id.paButton); pdButton = (Button) findViewById(R.id.pdButton); baButton = (Button) findViewById(R.id.baButton); bdButton = (Button) findViewById(R.id.bdButton); feedItems = new ArrayList<Property>(); listAdapter = new PropertyListAdapter(this, feedItems); listView.setAdapter(listAdapter); // Attach the listener to the AdapterView onCreate listView.setOnScrollListener( new EndlessScrollListener() { @Override public void onLoadMore(int page, int totalItemsCount) { findProperties(feedOrder, page); } }); // We first check for cached request Cache cache = AppController.getInstance().getRequestQueue().getCache(); Cache.Entry entry = cache.get(URL_FEED); if (entry != null) { // fetch the data from cache try { String data = new String(entry.data, "UTF-8"); try { parseJsonFeed(new JSONObject(data)); } catch (JSONException e) { e.printStackTrace(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } else { // First time you run the app there is no order and values are coming in default order. findProperties(feedOrder, pageCount); } paButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { pageCount = 0; feedOrder = "pa"; listView.setSelection(0); findProperties(feedOrder, pageCount); } }); pdButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { pageCount = 0; feedOrder = "pd"; listView.setSelection(0); findProperties(feedOrder, pageCount); } }); baButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { pageCount = 0; feedOrder = "ba"; listView.setSelection(0); findProperties(feedOrder, pageCount); } }); bdButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { pageCount = 0; feedOrder = "bd"; listView.setSelection(0); findProperties(feedOrder, pageCount); } }); }