Refresh listView every 20 seconds

1

I have a listView that I load from a web service. It works perfectly, now I want it to cool only every 20 seconds and for that I used the following code;

public class ListadoBC extends ActionBarActivity {

String ref = "";
String hora ="";
String op ="";
String equipo="";
int pulsado = -1;
int click = -1;
private ProgressDialog progressDialog;
ArrayList<BienCultural> listaBC = new ArrayList<BienCultural>();
ArrayList<Integer> bienes = new ArrayList<Integer>();

MyCustomAdapter dataAdapter = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listado_bc1);

    listado();
}


public void listado()
{
    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Descargando listado ...");
    progressDialog.setTitle("Progreso");
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progressDialog.setCancelable(false);

    ListadoAsyncTask listadoAsyncTask = new ListadoAsyncTask();
    listadoAsyncTask.execute("http://swa.hol.es/conectar1.php");
}

public void actualizarDisplay()
{
    progressDialog.dismiss();

    dataAdapter = new MyCustomAdapter(this,R.layout.bien_cultural, listaBC);
    final ListView listView = (ListView) findViewById(R.id.lista);

    listView.setAdapter(dataAdapter);

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {
            listado();
            dataAdapter.notifyDataSetChanged();
            handler.postDelayed(this, 20000); //now is every 2 minutes
        }
    }, 20000);
}

}

List saves the webService data in an ArrayList that is then passed to the adapter

It has been updated for 1 minute without problem, but suddenly it has given an error and closed the app. The logcat error is:

  

The content of the adapter has changed but ListView did not receive a   notification Make sure the content of your adapter is not modified   from a thread background, but only from the thread UI.

Any ideas?

    
asked by midlab 21.03.2017 в 23:19
source

1 answer

1
  

The content of the adapter has changed but ListView did not receive a   notification Make sure the content of your adapter is not modified   from a thread background, but only from the thread UI.

You are updating the data in ListView but from a thread in background, firstly ensure this process was only called once:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    public void run() {
        listado();
        handler.postDelayed(this, 20000); 
    }
}, 20000);

and to solve the problem

  

"The content of the adapter has changed but ListView did not receive a   notification "

, ensure you update the data using notifyDataSetChanged () :

 dataAdapter.notifyDataSetChanged();
    
answered by 21.03.2017 в 23:53