sending string value from classes extending BaseAdapter to Overlay
i am writing a code where i get a string value in a class that extends BaseAdapter. I want this value to be used in another class that extends 开发者_StackOverflow中文版an Overlay. If my class extends an Activity i can use intent,putstring() and getString, but is to be used for these above specified classes..Can anyone tell me how can i do this. Thanks in advance.
You can either make your variable global or make a singleton FileHelper class that contains values you want to pass between classes.
MyClass:
public String myString = "Hello";
and if you want to use it in OtherClass:
String myString = MyClass.myString;
If your the FileHelper use this:
public class FileHelper {
private static FileHelper instance;
private String myString;
private FileHelper() {
}
public static FileHelper getInstance() {
if (instance == null) {
instance = new FileHelper();
}
return instance;
}
public void setMyString(String s){
myString = s;
}
public String getMyString(){
return myString();
}
}
You can use the FileHelper with this:
private static FileHelper fileHelper = FileHelper.getInstance();
fileHelper.setString("hello");
String myString = fileHelper.getMyString();
精彩评论