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