2

In the code below, there's one activity that is fetching data from a mysql database (6 strings) via php to the android app and straight to a ListView.

The data fetch works fine, I just need the ListView to "automatically" refresh it self and also alert to the user that new data was fetched by sound and vibration.

Will be thankful for a solution, thanks in advance.

P.S: I know that I'm using deprecated methods that are from API 22 etc..

<?php

    define('HOST','my_hostname..');
    define('USERNAME','mysql username');
    define('PASSWORD','mysql password');
    define('DB','mysql db name');

    $con = mysqli_connect(HOST,USERNAME,PASSWORD,DB) or die('Unable to      

    connect');

    $sql = "select * from orders";

    $res = mysqli_query($con,$sql);

    $result = array();

   while($row = mysqli_fetch_array($res)){
   array_push($result,
   array('user'=>$row[0],'city'=>$row[1],'street'=>$row[2],          
   'streetnumber'=>$row[3],'phone'=>$row[4],'name'=>$row[5]));
   }

   echo json_encode(array("result"=>$result));

   mysqli_close($con);

   ?>

And the Android Studio Activity

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;


public class ViewOrdersActivity extends ActionBarActivity  {

    String myJSON;

    private static final String TAG_RESULTS="result";
    private static final String TAG_USER = "user";
    private static final String TAG_CITY = "city";
    private static final String TAG_STREET ="street";
    private static final String TAG_STREETNUMBER ="streetnumber";
    private static final String TAG_PHONE ="phone";
    private static final String TAG_NAME ="name";

    JSONArray ORDERS = null;

    ArrayList<HashMap<String, String>> ordersList;

    ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_orders);
        list = (ListView) findViewById(R.id.listView);
        ordersList = new ArrayList<HashMap<String,String>>();

        getData();

    }



    protected void showList(){
        try {
            JSONObject jsonObj = new JSONObject(myJSON);
            ORDERS = jsonObj.getJSONArray(TAG_RESULTS);

            for(int i=0;i<ORDERS.length();i++){
                JSONObject c = ORDERS.getJSONObject(i);
                String user = c.getString(TAG_USER);
                String city = c.getString(TAG_CITY);
                String street = c.getString(TAG_STREET);
                String streetnumber = c.getString(TAG_STREETNUMBER);
                String phone = c.getString(TAG_PHONE);
                String name = c.getString(TAG_NAME);


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

                orders.put(TAG_USER, user);
                orders.put(TAG_CITY, city);
                orders.put(TAG_STREET, street);
                orders.put(TAG_STREETNUMBER, streetnumber);
                orders.put(TAG_PHONE, phone);
                orders.put(TAG_NAME, name);


                ordersList.add(orders);
            }

            ListAdapter adapter = new SimpleAdapter(
                    ViewOrdersActivity.this, ordersList, R.layout.list_item,
                    new String[]{TAG_USER, TAG_CITY, TAG_STREET, TAG_STREETNUMBER, TAG_PHONE, TAG_NAME},
                    new int[]{R.id.user, R.id.city, R.id.street,R.id.streetnumber, R.id.phone , R.id.name}
            );

            list.setAdapter(adapter);
            list.invalidateViews();

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }


    public void getData(){
        class GetDataJSON extends AsyncTask<String, Void, String>{


            @Override
            protected String doInBackground(String... params) {

                DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
                HttpPost httppost = new HttpPost("http://myweb/folder/myphpfile.php");

                httppost.setHeader("Content-type", "application/json");

                InputStream inputStream = null;
                String result = null;
                try {
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();

                    inputStream = entity.getContent();
                    // json is UTF-8 by default
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();

                    String line = null;
                    while ((line = reader.readLine()) != null)
                    {
                        sb.append(line + "\n");
                    }
                    result = sb.toString();
                } catch (Exception e) {
                    // Oops
                }
                finally {
                    try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
                }
                return result;
            }

            @Override
            protected void onPostExecute(String result){
                myJSON=result;
                showList();
            }

        }

        GetDataJSON g = new GetDataJSON();
        g.execute();
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
iguana
  • 77
  • 1
  • 9
  • 1
    possible duplicate of [Repeat a task with a time delay?](http://stackoverflow.com/questions/6242268/repeat-a-task-with-a-time-delay) – Victory Sep 06 '15 at 14:42

3 Answers3

0
 public void refreshlistview(){  
 CountDownTimer mCountDownTimer = new CountDownTimer(60000, 1000) {
        public void onTick(long millisUntilFinished) {

        }
        public void onFinish() {
          //do refresh here 

          //then call it again - Recursive 
          refreshlistview();
        }
    } ;
 }

you can use timer and refresh every 1 mins = 60000 millisecond

to compare data i see you're using a hashmap on your showlist() so make it to return a hashmap and you can call it whenever you need to fetch new data and pass the old and new one to this method if it return true then the data is equal else you can inform the user that the data has been changed and set listview adapter again

public HashMap<String,String> compare(Map<String, String> old,Map<String,String> new)
{
    for (String i : old.keySet())
    {
        if (old.get(i) != new.get(i)) {
            return false;
        }
    } 
 return true;
}
A7med
  • 74
  • 1
  • 6
  • There's a problem, the user must get informed when ever new data is inserted, pasting the new data on the old one wont always tell me if a new data was inserted or not – iguana Sep 06 '15 at 15:28
  • you need to compare the adapter data for that , hold old adapter before fetching new one and compare them and show message after that if needed – A7med Sep 06 '15 at 15:44
  • is there any chance you can show me how to implement that mate? – iguana Sep 06 '15 at 15:52
0

This question can be done in two ways one is by TimerTask class and other is by Handler class. I prefer Handler one so i am posting how to do using Handler

http://developer.android.com/reference/java/util/TimerTask.html

final Handler handler = new Handler()
handler.postDelayed( new Runnable() {

    @Override
    public void run() {
        YOUR_ADAPTER_GOES_HERE.notifyDataSetChanged();
        //dont forget to call getData();
        handler.postDelayed( this, 60 * 10000 ); //ensure to run after 10 minutes
    }
}, 60 * 10000 );

You must only update UI in the main (UI) thread.

By creating the handler in the main thread, you ensure that everything you post to the handler is run in the main thread also.

  • hi there rewrihitesh, iv'e seen a thread with a similar comment, and I have a question: I need not only to refresh the data every X seconds I also need an indicator for a new inserted data just for notifying/alerting the AppUser that new data was inserted and he should check it out, after all this is a phone that isn't 24/7 infront of the client's eyes. so my question is , how can I solve this problem? – iguana Sep 06 '15 at 15:30
-2

Android WorkManager api is the best solution work manager