开发者

Android class to share data in activities problem

开发者 https://www.devze.com 2023-02-28 01:52 出处:网络
I have the following code with which I wou开发者_如何转开发ld like data to be stored in and returned true or false when needed:

I have the following code with which I wou开发者_如何转开发ld like data to be stored in and returned true or false when needed:

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;
        }
    }
}
}

How can I access data from other classes?

I now I have to set like this:

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

But I have no idea how to do this in ListAdapter

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

Am I on the right way to do it or is there any better way?

Thanks for help!


Just by doing

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

I get

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 is in Manifest

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


You're on the right track for this, you just need to put that manifest attribute under the Application tag, instead of an Activity tag (since DataHolder's not actually an Activity), like so:

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


I'd suggest using something like shared preferences for this, as the Android operating system can kill and restart your application at will, so if you just use a static variable it may get reset on you. Look at the documentation on data storage.

0

精彩评论

暂无评论...
验证码 换一张
取 消