How to get live updates via JSON to listview How to get live updates via JSON to listview json json

How to get live updates via JSON to listview


Well if your code above is working, you should be able to achieve your goal with the following steps:

  • Extract your JSON-Loading code into an AsyncTask, which you can trigger every minute. (AsyncTask)
  • Update your ListAdapter using the AsyncTask.


Put your JSON parsing code, that's probably your try..catch block in a separate function and not in onCreate().

So can easily call that part every minute. let's say that function name LoadData() also add one more line adapter.notifyDataSetChanged() to update adapter/list every time.

now in your onCreate(), write this code to call that function every one minute,

    final Handler handler = new Handler();    Runnable runable = new Runnable() {        @Override        public void run() {            //call the function            LoadData();            //also call the same runnable            handler.postDelayed(this, 1000);        }    };    handler.postDelayed(runable, 1000);

Now for second problem, to add new data at top..

I am just getting one thing in mind right now to write a loop , ADD THIS TO IN YOUR FUNCTION BEFORE CALLING ADAPTER NOTIFY CHANGE,like

     ArrayList<HashMap<String,String>> mylist = new  ArrayList<HashMap<String,String>>();      ArrayList<HashMap<String,String>> mylistTemp = new  ArrayList<HashMap<String,String>>();     for(int i = mylist.size()-1 ; i >=0;i--)     {         mylistTemp.add(mylist.get(i));     }     mylist = mylistTemp;


Here is a possible implementation of the onCreate() method, which will run an update-task every minute:

public class JSON_AndroidActivity extends Activity {    /** Called when the activity is first created. */    private JSONLoaderTask mJSONLoaderTask;    private Handler mHandler;    private ArrayAdapter<String> mArrayAdapter;    private Runnable mRefresher = new Runnable() {        @Override        public void run() {            mJSONLoaderTask = new JSONLoaderTask(JSON_AndroidActivity.this, mArrayAdapter);            mJSONLoaderTask.execute("");            mHandler.postDelayed(this, 1000);        }    };    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        mHandler.postDelayed(mRefresher, 1000);    }}

And here is a hull implementation of a possible task, where you can do your heavy loading and update your adapter for the list:

public class JSONLoaderTask extends AsyncTask<String, String, String> {    private Context context;    private ProgressDialog progressDialog;    private final ArrayAdapter<String> mArrayAdapter;    public JSONLoaderTask(Context pContext, ArrayAdapter<String> pArrayAdapter) {        this.context = pContext;        this.mArrayAdapter = pArrayAdapter;    }    @Override    protected void onPreExecute() {        progressDialog = new ProgressDialog(context);        progressDialog.show();    }    @Override    protected String doInBackground(String... params) {         String result = getJSONStream();        return result;    }    private String getJSONStream() {        //load your string here        return "";    }    @Override    protected void onPostExecute(String result) {        progressDialog.dismiss();        // Update your ArrayAdapter    }}