简体   繁体   中英

How to update a string's value in every place it is used

Like after adding some new data we're doing adapter.notifyDataSetChanged() how can we update string's value so that it would be changed in every place it's used

For example:

String s;//Declared at beginning
onCreate(){
    textview.setText(s);
}
public void change(){
    s="Hello";
}

Now how the value of s can be updated inside onCreate() I've tried to use s.notify() but it's showing following error

java.lang.IllegalMonitorStateException: object not locked by thread before notify()

The better pattern over here is implementing a Setter for s .

private String s;//Declared at beginning
private void setS(string s) {
    this.s = s;
    textView.setText(s);
}

onCreate(){
    setS("Initial Value") ;
}

public void change(){
    setS("Hello");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM