1

i'm trying to update the listview for every sec,so i used the handler and thread to update

i used the notifyDataSetChanged(); to reload the listview without refreshing the activity.

but my listview is not updating.

how can i update my listview repeatedly without refresh the activity

  private class MessageTask extends AsyncTask<String, Void, String> {
         private final HttpClient Client = new DefaultHttpClient();
        @Override
        protected String doInBackground(String... urls) {
            String output = "";
            for (String url : urls) {

                 try{
                 HttpGet httpget = new HttpGet(url);
                 ResponseHandler<String> responseHandler = new BasicResponseHandler();
                output = Client.execute(httpget, responseHandler);

                 }catch(Exception e){
                     Log.i("Animation", "Thread  exception " + e);
                 }
                 }
            return output;
        }



        protected void onPostExecute(String output) {




    try {


        JSONObject jObject= new JSONObject(output);
        JSONArray menuObject = new JSONArray(jObject.getString("response"));   
        ArrayList<String> tExp=new ArrayList<String>();
        //HashMap<String,ArrayList> map = new HashMap<String,ArrayList>();

     for (int i = 0; i<menuObject.length(); i++)
     {

         tExp.add(menuObject.getJSONObject(i).getString("fk_username_c").toString()+" "+menuObject.getJSONObject(i).getString("message_c").toString());




     }
     adapter=new ArrayAdapter<String>(ChatActivity.this,android.R.layout.simple_list_item_1);
     adapter.addAll(tExp);

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

         @Override
         public void run() {
             adapter.setNotifyOnChange(isChangingConfigurations());
             handler.postDelayed( this,  1000 );
         }
     },  1000 );

     listview.setAdapter(adapter);  

     } catch (JSONException e) {
         Log.e("log_tag", "Error parsing data " + e.toString());
     }

     //intent.putExtra(extras);



 }
}
user12378334
  • 117
  • 1
  • 14

1 Answers1

0

You are running updation of list in a separate thread which leads the execution of refreshing the list before even the list is modified or updated actually.

if you debeg your code then you can see that it is working fine because the thread got enough time to get executed.

Anchit Mittal
  • 3,412
  • 4
  • 28
  • 48