The code below currently pulls data from a mysql database and displays it in a ListView. What I am looking to do is find a way to have the application check the mysql database every minute or so to check for any new entries, and if it finds a new entry that isn't in the current ListView - it will prepend the new item(s) to the top of the list. I have read a few things on notifyDataSetChanged(), but I guess I can't grasp how it actually works or how to implement it. Any Help is appriciated. Thanks!

public class Data extends ListActivity {
    private ArrayList<Feed> posts = new ArrayList<Feed>();
    public void onCreate(Bundle savedInstanceState) {
        new FeedTask().execute();
    private class FeedTask extends AsyncTask<Void, Void, Void> {
        private ProgressDialog progressDialog;
        protected void onPreExecute() {
            progressDialog =,"", "Loading. Please wait...", true);
        protected Void doInBackground(Void... arg0) {
            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("http://xxxx/livefeed/getdata.php");
                HttpResponse httpResponse = httpClient.execute(httpPost);
                String result = EntityUtils.toString(httpResponse.getEntity());
                JSONArray jArray = new JSONArray(result);
                JSONObject json_data = null;
                for (int i = 0; i < jArray.length(); i++) {
                    json_data = jArray.getJSONObject(i);
                    Feed feed = new Feed();
                    feed.content = json_data.getString("post");
                    feed.time = json_data.getString("post_time");
            catch (Exception e){
                Log.e("ERROR", "Error loading JSON", e);
            return null;
        protected void onPostExecute(Void result) {
            setListAdapter(new FeedListAdaptor(Data.this, R.layout.feed, posts));
    private class FeedListAdaptor extends ArrayAdapter<Feed> {
        private ArrayList<Feed> posts;
        public FeedListAdaptor(Context context,
        int textViewResourceId,
        ArrayList<Feed> items) {
            super(context, textViewResourceId, items);
            this.posts = items;
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)
                v = vi.inflate(R.layout.feed, null);
            Feed o = posts.get(position);
            TextView tt = (TextView) v.findViewById(;
            TextView bt = (TextView) v.findViewById(;
            return v;
    public class Feed {
        String content;
        String time;

Discussed so many times on so many places example stackoverflow and many others, just Google

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.