Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: notifyDataSetChanged(); not working

I have a database in a server and from a Tablet I take some values from one table in the database. I load this information correctly into a list but I would like to know why when there is a change, nothing happens even if I use notifyDataSetChanged();. I must say that for loading the loading data y use the AsyncTaskClass So, my problem is that I don't know if use the notifyDataSetChanged(); method correctly ,because if there's is a change I would like to refresh the image. Here is some part of the code of the class:

@Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.all_candidatos);           candidatosList = new ArrayList<HashMap<String, String>>();          new CargarCandidatos().execute();     }   //  public void timer(){ //       new CountDownTimer(tiempo, 100) { // //              public void onTick(long millisUntilFinished) { //                   //              } // //              public void onFinish() { //              //  new CargarCandidatos().execute(); // //              } //           }.start();}        /**      * Background Async Task to Load all product by making HTTP Request      * */     class CargarCandidatos extends AsyncTask<String, String, String> {          /**          * Before starting background thread Show Progress Dialog          * */         @Override         protected void onPreExecute() {             super.onPreExecute();             pDialog = new ProgressDialog(Monitorizacion.this);             pDialog.setMessage("Loading ...");             pDialog.setIndeterminate(false);             pDialog.setCancelable(false);             pDialog.show();         }          /**          * getting All products from url          * */         protected String doInBackground(String... args) {             List<NameValuePair> params = new ArrayList<NameValuePair>();             JSONObject json = jParser.makeHttpRequest(url_candidatos, "GET", params);              Log.d("Candidatos: ", json.toString());              try {                 int success = json.getInt(TAG_SUCCESS);                  if (success == 1) {                      candidatos = json.getJSONArray(TAG_CANDIDATOS);                      for (int i = 0; i < candidatos.length(); i++) {                         JSONObject c = candidatos.getJSONObject(i);                          // Storing each json item in variable                         String nserie = c.getString(TAG_NSERIE);                         String dni = c.getString(TAG_DNI);                         String nombre = c.getString(TAG_NOMBRE);                         String test = c.getString(TAG_TEST);                         String pregunta = c.getString(TAG_PREGUNTA);                         String bateria = c.getString(TAG_BATERIA);                          // creating new HashMap                         HashMap<String, String> map = new HashMap<String, String>();                          // adding each child node to HashMap key => value                         map.put(TAG_NSERIE, nserie);                         map.put(TAG_DNI, dni);                         map.put(TAG_NOMBRE, nombre);                         map.put(TAG_TEST, test);                         map.put(TAG_PREGUNTA, pregunta);                         map.put(TAG_BATERIA, bateria);                          // adding HashList to ArrayList                         candidatosList.add(map);                     }                 }              } catch (JSONException e) {                 e.printStackTrace();             }              return null;         }          /**          * After completing background task Dismiss the progress dialog          * **/         protected void onPostExecute(String file_url) {             pDialog.dismiss();             runOnUiThread(new Runnable() {                 public void run() {                     /**                      * Updating parsed JSON data into ListView                      * */                     adapter = new SimpleAdapter(                             Monitorizacion.this, candidatosList,                             R.layout.list_item, new String[] { TAG_NSERIE,                                     TAG_DNI, TAG_NOMBRE, TAG_TEST, TAG_PREGUNTA, TAG_BATERIA},                             new int[] { R.id.id, R.id.dni, R.id.nombre, R.id.test, R.id.pregunta, R.id.bateria});                     setListAdapter(adapter);                     adapter.notifyDataSetChanged();                 //  timer();                 }             });          }      } } 
like image 681
Katherine99 Avatar asked Apr 23 '13 09:04

Katherine99


People also ask

How does notifyDataSetChanged work on Android?

Suppose your ListView displays some data stored in an ArrayList . After you change the contents of the ArrayList , you need to tell the list that the source of the data had changed and it needs to redraw itself to show the new data. So, that is where notifyDatasetChanged() comes in.

What does notifyDataSetChanged do in RecyclerView?

notifyDataSetChanged. Notify any registered observers that the data set has changed. There are two different classes of data change events, item changes and structural changes. Item changes are when a single item has its data updated but no positional changes have occurred.

How do you stop a list from responding to a user click?

just setClickable as false for the respective layout.


1 Answers

One of the main reasons notifyDataSetChanged() won't work for you - is,

Your adapter loses reference to your list.

When you first initialize the Adapter it takes a reference of your arrayList and passes it to its superclass. But if you reinitialize your existing arrayList it loses the reference, and hence, the communication channel with Adapter.

When creating and adding a new list to the Adapter. Always follow these guidelines:

  1. Initialise the arrayList while declaring it globally.
  2. Add the List to the adapter directly without checking for null and empty values. Set the adapter to the list directly (don't check for any condition). Adapter guarantees you that wherever you make changes to the data of the arrayList it will take care of it, but never lose the reference.
  3. Always modify the data in the arrayList itself (if your data is completely new then you can call adapter.clear() and arrayList.clear() before actually adding data to the list) but don't set the adapter i.e If the new data is populated in the arrayList than just adapter.notifyDataSetChanged()

Stay true to the Documentation.

like image 187
Sazzad Hissain Khan Avatar answered Oct 08 '22 03:10

Sazzad Hissain Khan