How do I show toast or toast every time my list increases or decreases?

0

I have a list that increases constantly every so often, I want to show a Toast when my list increases or decreases.

I was trying but I can not.

This is my code:

frutasList = new ArrayList<Datos>();

List<String> lables = new ArrayList<String>();

        for (int i = 0; i < frutasList.size(); i++) {
            lables.add(frutasList.get(i).getName() + " fecha" + frutasList.get(i).getFecha());
            dato.add(frutasList.get(i).getName() + "  fecha y hora:" + frutasList.get(i).getFecha());
            //lables.add(frutasList.get(i).getFecha());

        }
        if(this.frutasList.size()!=frutasList.size()){    Toast.makeText(MainActivity.this, "Cambio", Toast.LENGTH_SHORT).show();            }
        names = this.dato;

        mRecyclerView.setAdapter(mAdapter);

 codigo completo   _______________________________

// si presiono el item adname se actualizan los datos cada un segundo en base al los datos del servidor @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.add_name: this.addName(0); // si pulso el item adname se actualizan los datos return true; default: return super.onOptionsItemSelected(item); } }

private void addName(int position) {

StartTimer (); // the data is constantly being updated  }

private Handler handler;
private Runnable handlerTask;

void StartTimer(){
    handler = new Handler();
    handlerTask = new Runnable()
    {
        @Override
        public void run() {
            handler.postDelayed(handlerTask, 1000);  // se actualizan los datos cada un segundo
           updateData();  //  se llama al metodo actualizar
        }
    };
    handlerTask.run();
}

 //   metodo  actualizar

private void updateData () {

// Clear dato.. dato.clear(); // Refresh your listview.. populateSpinner(); frutasList = new ArrayList<Datos>(); List<String> lables = new ArrayList<String>(); for (int i = 0; i < frutasList.size(); i++) { lables.add(frutasList.get(i).getName() + " fecha" + frutasList.get(i).getFecha()); dato.add(frutasList.get(i).getName() + " fecha y hora:" + frutasList.get(i).getFecha()); //lables.add(frutasList.get(i).getFecha()); } /* quiero saber el aumento o disminucion de la lista **************************************************/ if(this.frutasList.size()!=frutasList.size()){ Toast.makeText(MainActivity.this, "Cambio", Toast.LENGTH_SHORT).show(); } names = this.dato; mRecyclerView.setAdapter(mAdapter); new Getfrutas().execute(); runOnUiThread(new Runnable() { @Override public void run() { mAdapter.notifyDataSetChanged(); } }); }

}

    
asked by Ayrton Axel 27.11.2017 в 02:38
source

2 answers

1

I think the best option is to declare 2 methods:

agregar(){
}

remover(){
}

In each method you do the corresponding operation of the list that you are modifying and at the end of the method you call:

Toast.makeText(getApplicationContext(), "La lista ha cambiado", Toast.LENGTH_SHORT).show();

The important thing is that you do not call the add methods of the array list directly.

    
answered by 27.11.2017 в 14:26
-1

From what I see the array dato is the contains the list that loads in the listView, so the size of this is the one that you have to evaluate to know if the list has increased in size or not.

To do this, get the size of the array before and after updating it. Then compare the two sizes in a conditional and according to the result you throw a Toast .

// obtienes el tamaño de la lista antes de actualizarla
int sizeAntes = dato.size();

dato.clear();

populateSpinner();
frutasList = new ArrayList<Datos>();
List<String> lables = new ArrayList<String>();

for (int i = 0; i < frutasList.size(); i++) {
    lables.add(frutasList.get(i).getName() + " fecha" + frutasList.get(i).getFecha());
    dato.add(frutasList.get(i).getName() + "  fecha y hora:" + frutasList.get(i).getFecha());
}

// obtienes el tamaño de la lista después de actualizarla
int sizeDespues = dato.size(); 

// Evalúas el tamaño de la lista: si el valor de sizeAntes es menor al de sizeDespués 
// la condición se cumple. Lo que quiere decir que el tamaño de la lista es mayor, de lo
// contrario la condición no se cumple, por lo cual el tamaño de la lista es menor.
if (sizeAntes < sizeDespues) {
    Toast.makeText(getApplicationContext(), "El tamaño de la lista es mayor", Toast.LENGTH_SHORT).show();
} else {
    Toast.makeText(getApplicationContext(), "El tamaño de la lista es menor", Toast.LENGTH_SHORT).show();
}
    
answered by 27.11.2017 в 15:38