在Java(Android应用程序)中,在一个类中设置值并从另一个类中检索
当用户在应用程序启动时输入id时,我正在从我的主类设置id。考虑到这一点,我在一个名为GetSet.java的类中设置了它,该类也有一个方法在被调用时返回它。所以我需要一个名为MySQLitehelper.java的类中第一个类的id集,这样我就可以根据id集而不是硬编码来检索数据。这将使我的应用程序成为动态的,而不是静态的。这是我的密码: /----MainActivity.java-----/ /---MySQLitehelper.java---/在Java(Android应用程序)中,在一个类中设置值并从另一个类中检索,java,android,Java,Android,当用户在应用程序启动时输入id时,我正在从我的主类设置id。考虑到这一点,我在一个名为GetSet.java的类中设置了它,该类也有一个方法在被调用时返回它。所以我需要一个名为MySQLitehelper.java的类中第一个类的id集,这样我就可以根据id集而不是硬编码来检索数据。这将使我的应用程序成为动态的,而不是静态的。这是我的密码: /----MainActivity.java-----/ /---MySQLitehelper.java---/ 我在上面底部类的getRecord()方法
我在上面底部类的getRecord()方法中检索它,而不是
COLUMN_ID+”=47688507
我希望从GetSet
类(getId
方法)中获取设置值并将其替换为47688507
。有什么建议吗?我认为,您应该能够使用实例变量obj_getset
来设置id
obj\u getset.setid(mCursor.COLUMN\u ID)
在getRecord
函数中,然后在需要时以相同的方式检索它
obj_getset.getID()
我认为,您应该能够使用实例变量obj_getset
来设置id
obj\u getset.setid(mCursor.COLUMN\u ID)
在getRecord
函数中,然后在需要时以相同的方式检索它
obj\u getset.getID()
- 使用全局单例存储和检索。执行此操作时,请确保该单例未包含对您的活动或上下文的引用
- 因为mysqlitehelper有一个对上下文的引用,所以您还可以从SharedReferences检索持久化数据 -
- 使用全局单例存储和检索。执行此操作时,请确保该单例未包含对您的活动或上下文的引用
- 因为mysqlitehelper有一个对上下文的引用,所以您还可以从SharedReferences检索持久化数据 -
/**
* This is a singleton class to hold preferences.
*/
public class Prefs {
private static volatile Prefs instance = null;
// The values held by this class
String identity = null;
String apiUrl = "http://example.com/cgi-bin/api";
int interval = 3600; // when there's no activity
/**
* Return the single instance of this class, creating it
* if necessary. This method is thread-safe.
*/
public static Prefs getPrefs(Context ctx) {
if (instance == null) {
synchronized(Prefs.class) {
if (instance == null) {
instance = new Prefs();
instance.readPrefs(ctx);
}
}
}
return instance;
}
/**
* Re-read all preferences (you never need to call this explicitly)
*/
private void readPrefs(Context ctx) {
try {
SharedPreferences sp =
PreferenceManager.getDefaultSharedPreferences(ctx);
identity = sp.getString("identity", identity);
apiUrl = sp.getString("apiUrl", apiUrl);
interval = sp.getInt("interval", interval);
} catch (Exception e) {
Log.e(TAG, "exception reading preferences: " + e, e);
// TODO: report it
}
}
/**
* Save preferences; you can call this from onPause()
*/
public void savePrefs(Context ctx) {
try {
SharedPreferences.Editor sp =
PreferenceManager.getDefaultSharedPreferences(ctx).edit();
sp.putString("identity", identity);
sp.putString("apiUrl", apiUrl);
sp.putInt("interval", interval);
sp.commit();
} catch (Exception e) {
Log.e(TAG, "exception reading preferences: " + e, e);
// TODO: report it
}
}
/**
* Save preferences to a bundle. You don't really need to implement
* this, but it can make start-up go faster.
* Call this from onSaveInstanceState()
*/
public void onSaveInstanceState(Bundle state) {
state.putString("identity", identity);
state.putString("apiUrl", apiUrl);
state.putInt("interval", interval);
}
/**
* Recall preferences from a bundle. You don't really need to implement
* this, but it can make start-up go faster.
* Call this from onCreate()
*/
public void restoreInstanceState(Bundle state) {
identity = state.getString("identity");
apiUrl = state.getString("apiUrl");
interval = state.getInt("interval");
}
}
请阅读“Singleton模式”,这是一种创建持久对象的方法,用于保存从一个位置传递到另一个位置的信息
但是,请记住,一旦已在您的活动中被调用,系统可以完全销毁您的应用程序,并在以后重新构建它。这意味着即使是单例类也不能保证在应用程序重新启动时您的数据会在那里。您必须随时准备在onCreate()方法中从持久性存储恢复您的状态
下面是我如何使用singleton类实现全局首选项持有者:简而言之,在任何有上下文的地方,都可以调用Prefs.getPrefs()获取preferences对象。在第一次调用时,它将从持久存储中读取首选项。在所有后续调用中,它只返回指向已加载对象的指针。如果应用程序被系统终止并重新启动,则getPrefs()会根据需要无缝地重新读取首选项
/**
* This is a singleton class to hold preferences.
*/
public class Prefs {
private static volatile Prefs instance = null;
// The values held by this class
String identity = null;
String apiUrl = "http://example.com/cgi-bin/api";
int interval = 3600; // when there's no activity
/**
* Return the single instance of this class, creating it
* if necessary. This method is thread-safe.
*/
public static Prefs getPrefs(Context ctx) {
if (instance == null) {
synchronized(Prefs.class) {
if (instance == null) {
instance = new Prefs();
instance.readPrefs(ctx);
}
}
}
return instance;
}
/**
* Re-read all preferences (you never need to call this explicitly)
*/
private void readPrefs(Context ctx) {
try {
SharedPreferences sp =
PreferenceManager.getDefaultSharedPreferences(ctx);
identity = sp.getString("identity", identity);
apiUrl = sp.getString("apiUrl", apiUrl);
interval = sp.getInt("interval", interval);
} catch (Exception e) {
Log.e(TAG, "exception reading preferences: " + e, e);
// TODO: report it
}
}
/**
* Save preferences; you can call this from onPause()
*/
public void savePrefs(Context ctx) {
try {
SharedPreferences.Editor sp =
PreferenceManager.getDefaultSharedPreferences(ctx).edit();
sp.putString("identity", identity);
sp.putString("apiUrl", apiUrl);
sp.putInt("interval", interval);
sp.commit();
} catch (Exception e) {
Log.e(TAG, "exception reading preferences: " + e, e);
// TODO: report it
}
}
/**
* Save preferences to a bundle. You don't really need to implement
* this, but it can make start-up go faster.
* Call this from onSaveInstanceState()
*/
public void onSaveInstanceState(Bundle state) {
state.putString("identity", identity);
state.putString("apiUrl", apiUrl);
state.putInt("interval", interval);
}
/**
* Recall preferences from a bundle. You don't really need to implement
* this, but it can make start-up go faster.
* Call this from onCreate()
*/
public void restoreInstanceState(Bundle state) {
identity = state.getString("identity");
apiUrl = state.getString("apiUrl");
interval = state.getInt("interval");
}
}
setid和getID都在不同的类中。我在MainActivity中设置了它。但我需要在SelectOptions.java类中获取它。我可能缺少一些内容,但如果在调用intent()的同一活动中设置它函数,为什么不能将id作为参数传递给该函数,并将其作为附加值发送给该函数?setid和getID都在不同的类中。我在MainActivity中设置了它。但我需要在SelectOptions.java类中获取它。我可能缺少一些内容,但如果在调用intent()的同一活动中设置了它函数,为什么您不能将id作为参数传递给该函数,并将其作为附加值发送?我在应用程序中使用了类似的实现。很高兴知道我不是唯一得出此结论的人。我在应用程序中使用了类似的实现。很高兴知道我不是唯一得出此结论的人。
/**
* This is a singleton class to hold preferences.
*/
public class Prefs {
private static volatile Prefs instance = null;
// The values held by this class
String identity = null;
String apiUrl = "http://example.com/cgi-bin/api";
int interval = 3600; // when there's no activity
/**
* Return the single instance of this class, creating it
* if necessary. This method is thread-safe.
*/
public static Prefs getPrefs(Context ctx) {
if (instance == null) {
synchronized(Prefs.class) {
if (instance == null) {
instance = new Prefs();
instance.readPrefs(ctx);
}
}
}
return instance;
}
/**
* Re-read all preferences (you never need to call this explicitly)
*/
private void readPrefs(Context ctx) {
try {
SharedPreferences sp =
PreferenceManager.getDefaultSharedPreferences(ctx);
identity = sp.getString("identity", identity);
apiUrl = sp.getString("apiUrl", apiUrl);
interval = sp.getInt("interval", interval);
} catch (Exception e) {
Log.e(TAG, "exception reading preferences: " + e, e);
// TODO: report it
}
}
/**
* Save preferences; you can call this from onPause()
*/
public void savePrefs(Context ctx) {
try {
SharedPreferences.Editor sp =
PreferenceManager.getDefaultSharedPreferences(ctx).edit();
sp.putString("identity", identity);
sp.putString("apiUrl", apiUrl);
sp.putInt("interval", interval);
sp.commit();
} catch (Exception e) {
Log.e(TAG, "exception reading preferences: " + e, e);
// TODO: report it
}
}
/**
* Save preferences to a bundle. You don't really need to implement
* this, but it can make start-up go faster.
* Call this from onSaveInstanceState()
*/
public void onSaveInstanceState(Bundle state) {
state.putString("identity", identity);
state.putString("apiUrl", apiUrl);
state.putInt("interval", interval);
}
/**
* Recall preferences from a bundle. You don't really need to implement
* this, but it can make start-up go faster.
* Call this from onCreate()
*/
public void restoreInstanceState(Bundle state) {
identity = state.getString("identity");
apiUrl = state.getString("apiUrl");
interval = state.getInt("interval");
}
}