2011-04-21 49 views
1

数据我已经被存储在以下代码,我想数据,并在需要时返回true或false:Android的学生共享活动问题

public class DataHolder extends Application { 
private long NextRestoreTime = 0; 

public boolean getNextResotreTime() 
{ 
    if (System.currentTimeMillis() >= NextRestoreTime) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 
public void setRestoreTime(long RestoreTime) 
{ 
    if(NextRestoreTime == 0) 
    { 
     NextRestoreTime = RestoreTime; 
    } 
    else 
    { 
     if(RestoreTime < NextRestoreTime) 
     { 
      NextRestoreTime = RestoreTime; 
     } 
    } 
} 
} 

如何从其他类访问数据吗?

我现在我要这样设置:

DataHolder dataHolder = ((DataHolder)getApplicationContext());

但我不知道如何做到这一点的ListAdapter

public class CurrentAdapter extends ArrayAdapter<CurrentlyItem> { ...

我是在做正确的方式它还是有没有更好的办法?

感谢您的帮助!


如果这样做的

DataHolder dataHolder = ((DataHolder)getApplicationContext());

我得到

04-21 14:40:24.361: ERROR/AndroidRuntime(697): FATAL EXCEPTION: main 
04-21 14:40:24.361: ERROR/AndroidRuntime(697): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.TVSpored/com.TVSpored.Currently}: java.lang.ClassCastException: android.app.Application 
04-21 14:40:24.361: ERROR/AndroidRuntime(697):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
04-21 14:40:24.361: ERROR/AndroidRuntime(697):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503) 
04-21 14:40:24.361: ERROR/AndroidRuntime(697):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127) 
04-21 14:40:24.361: ERROR/AndroidRuntime(697):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339) 
04-21 14:40:24.361: ERROR/AndroidRuntime(697):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651) 
04-21 14:40:24.361: ERROR/AndroidRuntime(697):  at android.widget.TabHost.setCurrentTab(TabHost.java:323) 
04-21 14:40:24.361: ERROR/AndroidRuntime(697):  at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129) 
04-21 14:40:24.361: ERROR/AndroidRuntime(697):  at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453) 
04-21 14:40:24.361: ERROR/AndroidRuntime(697):  at android.view.View.performClick(View.java:2408) 
04-21 14:40:24.361: ERROR/AndroidRuntime(697):  at android.view.View$PerformClick.run(View.java:8816) 
04-21 14:40:24.361: ERROR/AndroidRuntime(697):  at android.os.Handler.handleCallback(Handler.java:587) 
04-21 14:40:24.361: ERROR/AndroidRuntime(697):  at android.os.Handler.dispatchMessage(Handler.java:92) 
04-21 14:40:24.361: ERROR/AndroidRuntime(697):  at android.os.Looper.loop(Looper.java:123) 
04-21 14:40:24.361: ERROR/AndroidRuntime(697):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
04-21 14:40:24.361: ERROR/AndroidRuntime(697):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-21 14:40:24.361: ERROR/AndroidRuntime(697):  at java.lang.reflect.Method.invoke(Method.java:521) 
04-21 14:40:24.361: ERROR/AndroidRuntime(697):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
04-21 14:40:24.361: ERROR/AndroidRuntime(697):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
04-21 14:40:24.361: ERROR/AndroidRuntime(697):  at dalvik.system.NativeStart.main(Native Method) 
04-21 14:40:24.361: ERROR/AndroidRuntime(697): Caused by: java.lang.ClassCastException: android.app.Application 
04-21 14:40:24.361: ERROR/AndroidRuntime(697):  at com.TVSpored.Currently.onCreate(Currently.java:47) 
04-21 14:40:24.361: ERROR/AndroidRuntime(697):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
04-21 14:40:24.361: ERROR/AndroidRuntime(697):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
04-21 14:40:24.361: ERROR/AndroidRuntime(697):  ... 18 more 

活动是在清单

<activity android:name=".DataHolder"></activity>

回答

0

你是在正确的轨道为此,你只需要把该清单属性的应用程序标记下,而不是活动标签(因为DataHolder实际上不是一个Activity),像这样:

<application android:name=".DataHolder" //...:icon and :label to follow > 
+0

例如,通过存储整个ListView来工作吗?我在旋转ListView时遇到问题... – 2011-04-22 07:02:53

+0

不,这完全没有关系。这只是为了存储变量,你的ListViews是你的活动的一部分。您需要处理方向更改以保持它们之间的数据,因为活动被破坏并在方向更改时重新创建。这应该有所帮助:http://stackoverflow.com/questions/2590162/android-listview-in-activity-getting-refreshed-on-changing-orientation/2592289#2592289 – kcoppock 2011-04-22 13:50:55

0

我建议使用像这样的共享首选项的东西,因为Android操作系统可以杀死并重新启动你的应用程序,所以如果你只是使用一个静态变量它可能会重置你。看看documentation on data storage