One way to solve this is to use SharedPreferences
to save your counter number. First instancie:
SharedPreferences config = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = config.edit();
Then on the part of for
I would replace it with a do while
and take the amount saved in SharedPreferences
to perform the loop. It would look like this:
int contador = config.getInt("contador", 1);
do {
testes.add("Data: "+myDB.getDates(contador)+" "+myDB.getHour(contador)+"\n"+"Discilpina: "+myDB.getDisciplina(contador));
contador++;
} while (contador<=myDB.getLastId());
editor.putInt("contador", contador);
ListAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,testes);
testList.setAdapter(adapter);
Note that before the do-while
the counter is obtained unless and if there is no number1
. Then the value of the counter is saved through the editor
. And as acklay commented, remove the adapter
from inside the loop.
Perhaps it would be better to use a static variable with the counter or change the architecture of your project to a more systematic way but what I went through should solve.
Why the
adapter
is inside thefor
?! There’s something wrong there that’s not right.– viana
I’ve already pulled the Adapter out of the room and keep repeating it. Because this cycle is started when Activity is indicated, which by chance is often why Arraylist tests will always be added values
– Emanuel Sobreiro
I wanted the value of the counter to remain constant
– Emanuel Sobreiro