Solution 1 :
This statement, List<String> parkings = new ArrayList<>();
is creating a new empty list every time listPopulate is called. You need to move List<String> parkings = new ArrayList<>();
outside of the listPopulate method in order to preserve the ArrayList you’re creating.
example
public List<String> parking;
public void onCreate(xxxxxxxxxxxx){
XXXXXXXXXXXXXXX
parking = new ArrayList<>();
}
public void listPopulate(xxxx){
xxxxxxx
xxxxxxx
xxxxxx
}
If you want to preserve the list after closing the app, you’ll need to cache the list locally on the device.
Problem :
I want to populate my list view every time a value is changed with the changed value
The code for ValueEventListener is as
ValueEventListener changeListener3 = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String carNumber = dataSnapshot.getValue(String.class);
listPopulate(carNumber);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
};
And the function to populate the value is
public void listPopulate(String carNumber)
{
List<String> parkings = new ArrayList<>();
parkings.add(carNumber);
final ListView listView = findViewById(R.id.listView2);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,parkings);
listView.setAdapter(arrayAdapter);
}
But when I run it the value is updated in listView but it simply replace the current value I want to keep the last value along with new update data