How to prevent a listView from returning to the beginning when updating its adapter?

3

My problem is as follows. I have a ListView that is completed from data obtained in a mysql query by means of volley. This task is performed once per second by means of a timerTask. The ListView returns to the first item once per second (each time the new adapter is loaded), making it impossible to work with the last ListView values, when scrolling. I attach the code.

TimerTask task = new TimerTask() {
    @Override
    public void run() {
        handler2.post(new Runnable() {
            public void run() {
                String iip = "192.168.1.34";
                String param = comandaActiva.getIdComanda();
                String consulta = "http://" + iip + "/Antonia/consultaComanda.php?id=" + param;

                EnviarRecibirDatos(consulta);
            }
        });
    }
};
timer.schedule(task, 100, 1000);  //ejecutar en intervalo de 1 segundo.


private RequestQueue queue;
private JSONArray ja;
public void EnviarRecibirDatos(String URL){

    if (queue == null) {
        queue = Volley.newRequestQueue(this);

    }


    StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            //Toast.makeText(getApplicationContext(), "on response", Toast.LENGTH_SHORT).show();
            ja = new JSONArray();
            response = response.replace("][",",");
            if (response.length()>0){
                //Toast.makeText(getApplicationContext(), "response>0", Toast.LENGTH_SHORT).show();
                try {
                    ja = new JSONArray(response);
                    if(ja.length()>0){

                    //Log.i("sizejson",""+ja.length());
                    CargarListView(ja);}
                    else
                        {



                        }

                } catch (JSONException e) {


                }

            }


        }
    }, new Response.ErrorListener(){
        @Override
        public void onErrorResponse(VolleyError error) {


        }
    });

    queue.add(stringRequest);
}

private ArrayList<String> lista;
ArrayAdapter<String> adaptador;
public void CargarListView(JSONArray ja){

    lista = new ArrayList<>();
    for (int h = 0 ; h<lista.size();h++)
    {
        lista.remove(h);
    }
    if(ja.length()>0){
    for(int i=0;i<ja.length();i+=4){

        try {
            String temporal=ja.getString(i+1);

            lista.add(ja.getString(i)+") "+ja.getString(i+2)+"X"+temporal+"\n"+ja.getString(i+3)+" ");
        } catch (Exception e) {
            lista.clear();
            lista.add("");
        }

    }}
    else
        {
            for(int r=0; r<lista.size();r++){
                lista.remove(r);}
        }

    int scroll = lvProductos.getMaxScrollAmount();



    adaptador = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lista);

    lvProductos.setAdapter(adaptador);
}

Thanks in advance. Greetings

    
asked by fabri 20.03.2018 в 20:27
source

1 answer

1

There are 2 options,

1) You can get the current position of your first visible view in the list

int firstPosition = listView.getFirstVisiblePosition();

and use this position to move your listView to that position:

listView.setSelection(firstPosition);

2) you can use the method:

  

notifyDataSetChanged () Notifies attached observers that   the underlying data has been changed and that any view that   reflect the data set should be updated

In the CargarListView() method you are loading the "new" data at Adapter , therefore use notifyDataSetChanged()

public void CargarListView(JSONArray ja){    
  ...
  ...
  ...
    adaptador = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lista);

    lvProductos.setAdapter(adaptador);

    adaptador.notifyDataSetChanged();

}
    
answered by 21.03.2018 в 17:48