MjBodz MjBodz - 2 years ago 141
Android Question

Pull Refresh/Swipe Down on Existing Android Project in Eclipse

Can someone help me to add a Pull Refresh on my Project?

Here is The output of my Project:

enter image description here

That Output came From MySQL database using this php/Json Codes:


mysqli_select_db($con,"qstatslite")or die("error");

$q = mysqli_query($con,"SELECT * FROM queue_stats ORDER BY queue_stats_id DESC LIMIT 20");

$return_arr = array();

while ($row = mysqli_fetch_array($q)) {
//$return_arr['queue_stats_id'] = $row['queue_stats_id'];
$row_array['datetime'] = $row['datetime'];
$row_array['qname'] = $row['qname'];
$row_array['qagent'] = $row['qagent'];
$row_array['qevent'] = $row['qevent'];
$row_array['info1'] = $row['info1'];
$row_array['info2'] = $row['info2'];
$row_array['info3'] = $row['info3'];

//$return_arr['queue'][] = array('name' = $tableName, 'queue' = $rows);


$output = json_encode(array('queue' => $return_arr));
echo $output;


My Java Codes is here :

public class MainActivity extends ListActivity{

private ProgressDialog pDialog;
private static String IPaddress = "";

// URL to get contacts JSON
private static String url = IPaddress + "Projects/GetUsers.php";

private static final String TAG_QUEUE = "queue";
private static final String TAG_DATETIME = "datetime";
private static final String TAG_NAME = "qname";
private static final String TAG_AGENT = "qagent";
private static final String TAG_EVENT = "qevent";
private static final String TAG_INFO1 = "info1";
private static final String TAG_INFO2 = "info2";
private static final String TAG_INFO3 = "info3";

// contacts JSONArray
JSONArray queue = null;

// Hashmap for ListView
ArrayList<HashMap<String, String>>queueList;

public void onCreate(Bundle savedInstanceState) {

queueList = new ArrayList<HashMap<String, String>>();

ListView lv = getListView();

// Calling async task to get json
new GetEventCounter().execute();

* Async task class to get json by making HTTP call
* */
private class GetEventCounter extends AsyncTask<Void, Void, Void> {

protected void onPreExecute() {
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity2.this);
pDialog.setMessage("Please wait...");


protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();

// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

Log.d("Response: ", "> " + jsonStr);

if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);

queue = jsonObj.getJSONArray(TAG_QUEUE);

for (int i = 0; i < queue.length(); i++) {
JSONObject c = queue.getJSONObject(i);

String datetime = String.format("%s : %s", TAG_DATETIME, c.getString(TAG_DATETIME));
String qname = String.format("%s : %s", TAG_NAME, c.getString(TAG_NAME));
String qagent = String.format("%s : %s", TAG_AGENT, c.getString(TAG_AGENT));
String qevent = String.format("%s : %s", TAG_EVENT, c.getString(TAG_EVENT));
String info1 = String.format("%s : %s", TAG_INFO1, c.getString(TAG_INFO1));
String info2 = String.format("%s : %s", TAG_INFO2, c.getString(TAG_INFO2));
String info3 = String.format("%s : %s", TAG_INFO3, c.getString(TAG_INFO3));

HashMap<String, String> event = new HashMap<String, String>();

event.put(TAG_DATETIME, datetime);
event.put(TAG_NAME, qname);
event.put(TAG_AGENT, qagent);
event.put(TAG_EVENT, qevent);
event.put(TAG_INFO1, info1);
event.put(TAG_INFO2, info2);
event.put(TAG_INFO3, info3);

} catch (JSONException e) {
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");

return null;

protected void onPostExecute(Void result) {
// Dismiss the progress dialog
if (pDialog.isShowing())
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity2.this, queueList,
R.layout.list_item, new String[] { TAG_DATETIME, TAG_NAME, TAG_AGENT, TAG_EVENT, TAG_INFO1, TAG_INFO2, TAG_INFO3},
new int[] { R.id.datetime, R.id.qname, R.id.qagent, R.id.qevent, R.id.info1, R.id.info2, R.id.info3 });




What is needed to add that if I swipe down it will Refresh automatically? So that it will show the new updated details from Database.
Your Help will be much appreciated! Thanks!

Answer Source

There's lots of info out there but I'll tell you what you need to know. Your activity (or fragment w/e) needs to implement the OnRefreshListener.

This will of course lead you to implement the onRefresh() method. The method will be called when you swipe down, and can also be called whenever you want (for example, a refresh button in the ActionBar can call it).

public class MainActivity extends ListActivity implements SwipeRefreshLayout.OnRefreshListener{

    public void onRefresh(){
        //do something

You will also need to add a SwipeRefreshLayout to your layout xml.


You'll also need to call swipeRefreshLayout.setOnRefreshListener(this) in your onCreateView method after you've assigned your swipeRefreshLayout view

Developer's Guide to SwipeRefreshLayouts

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download