Android - Update ArrayAdapter from a new array

1

I need to know how to update a ListView from an own ArrayAdapter by means of the array that I sent it, I explain:
I have this JSON:

[{"id":"1","user":"117270122","mensaje":"Mensaje de prueba","condicion_visto":"0"},{"id":"2","user":"117270122","mensaje":"Mensaje","condicion_visto":"0"},{"id":"3","user":"117270122","mensaje":"Otro mensaje","condicion_visto":"0"}]  

Which I extracted from the database and converted it into an ArrayList here:

public ArrayList<String> fillArray(){
    final ArrayList<String> res = new ArrayList<String>();
    Thread th = new Thread(new Runnable() {
        @Override
        public void run() {
            try{
                JSONArray nodo = new JSONArray(db.verificarDatosNuevos(user));//El JSON lo extraigo de la base de datos
                for (int i = 0; i < nodo.length(); i++) {
                    JSONObject json = nodo.getJSONObject(i);
                    String usuario = json.getString("user");
                    String mensaje = json.getString("mensaje");
                    String output = "Para: " + usuario + "\nMensaje: " + mensaje;
                    res.add(output);
                }
                System.out.println("NODO FINAL"+nodo.toString());
            }catch(Exception e){
                e.printStackTrace();
            }
            System.out.println("ARRAY FINAL"+res);
        }
    });
    th.start();
    try {
        th.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("ARRAY FINAL 2"+res);
    return res;
}  

Later I'll call him on the OnCreate:

Adaptador ad = new Adaptador(Principal.this,R.layout.lista_mensajes,list,a);
ListView lv = findViewById(R.id.listaMensajes);
lv.setAdapter(ad);  

The code of my adapter is as follows:

public class Adaptador extends ArrayAdapter<String>{
    ArrayList<String> arr;
    private TextView text;
    private String usuario = "";
    JsonParser j;
    public Adaptador(Context context, int textViewResourceId,ArrayList<String> objects,String c){
        super(context,textViewResourceId,objects);
        this.arr = objects;
        System.out.println("CONSTRUCTOR: "+arr.size());
        this.usuario = c;
        this.j = new JsonParser(usuario);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        System.out.println("VIEW: "+arr.size());
        View view = null;
        LayoutInflater inflater = getLayoutInflater();
        view = inflater.inflate(R.layout.lista_mensajes,parent,false);
        TextView textView = view.findViewById(R.id.textoMensaje);
        textView.setText(arr.get(position));
        notifyDataSetChanged();
        return view;
    }

    public void actualizar(ArrayList<String> listaNueva){
        //this.arr.clear();
        this.arr = listaNueva;
        System.out.println("ACTUALIZAR: "+arr.toString());
        this.notifyDataSetChanged();
    }

}  

The problem is that in a button when I call the update function, it is not updated:

btnref.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            list = j.fillArray();
            ad.notifyDataSetChanged();
            ad.actualizar(list);
        }
    });

When the database changes, the json changes, but I do not know how to make the adapter recognize it and change the list, or is there any other way to update the list?

    
asked by Daylight Ark 16.05.2018 в 00:19
source

1 answer

1

In order to update the list, you can not notify a reference change in memory of it, but modify its elements.

In a few words you can not do: this.lista = j.fillArray();

The Adapter is populated with a reference. When started, you can not change the reference of that list and that's why when calling notifyDataSetChanged, nothing happens. What you must do is modify the one you already have, removing , adding , cleaning it or inserting elements in it. If you want to change one or all of the elements, notifyDataSetChanged is used, so you should modify your list in the following way:

btnref.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            list.clear(); // Limpia la lista anterior
            list.addAll(j.fillArray()); // agrega los nuevos elementos
            ad.notifyDataSetChanged();
        }
    });

Just do that to update your list. You must remove the method

public void actualizar(ArrayList<String> listaNueva){
        //this.arr.clear();
        this.arr = listaNueva;
        System.out.println("ACTUALIZAR: "+arr.toString());
        this.notifyDataSetChanged();
    }

and all other calls to notifyDataSetChanged(); other than the onClick .

    
answered by 16.05.2018 / 00:29
source