I want to detect change in my ArrayList myList
. Everytime I add, remove or update anything from ArrayList. I want to notify user about it. I have used following code for it. It notifies user when setValues
function is used to set list. Is there any other way to implement it so that every time I change list user will get notified? Thank you.
// observable class which will contain my List.
package com.psl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Observable;
public class MyList extends Observable{
private List<Map<String,Object>> values;
public List<Map<String, Object>> getValues() {
return values;
}
public void setValues(List<Map<String, Object>> values) {
if(getValues()==null && values!=null){
setChanged();
notifyObservers();
}
else if( !this.values.equals(values)){
setChanged();
notifyObservers();
}
this.values = values;
}
public static void main(String[] args) {
MyList myList = new MyList();
List<Map<String, Object>> values = new ArrayList<Map<String, Object>>();
Notify notify = new Notify();
myList.addObserver(notify);
Map<String, Object> map = new HashMap<String, Object>();
map.put("string_value", null);
myList.setValues(values);
}
}
// Observer which will notify user when list is updated.
package com.psl;
import java.util.Observable;
import java.util.Observer;
public class Notify implements Observer{
@Override
public void update(Observable o, Object arg) {
System.out.println("List has been changed");
}
}
JavaFX has an implementation of ObservableList, maybe you can use it:
javafx.collections.ObservableList a = javafx.collections.FXCollections.observableArrayList();
a.addListener(new javafx.collections.ListChangeListener<String>() {
@Override
public void onChanged(Change<? extends String> c) {
System.out.println(c);
}});
a.add("aa");
a.add("bb");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With