accessing array values across classes
I have an activity which includes a declaratio开发者_C百科n of
public String progressArray[][] = new String[3][3]
In another file (a service) I want to access the values of this array. I code:
ListViewLoader lvl = new ListViewLoader
Now I find that lvl.progressArray[0][0]
returns null although I know that data has been put in it in its own class. Is this because a new instance doesnt contain the values of the original, if so how can I access the values in the original?
Your solution is not working, because progressArray is an instance variable and while calling
ListViewLoader lvl = new ListViewLoader
which creates new instance with progressArray set to default value (null in that case). Making progressArray static can be a fast solution, but be aware you will highly depend on order in which activity and service will access that array and it may be reset when your activity is destroyed by system. I'm strongly discouraging you from doing that. Instead, please consider extending Application class and implement list initialisation / access there, like:
public class MyApplication extends Application {
public String progressArray[][] = new String[3][3];
}
Then declare it in AndroidManifest like:
<application
android:name=".MyApplication"
android:label="@string/app_name"
android:icon="@drawable/icon">
Then both, from your activities and services you can get that by calling
((MyApplication) getApplication()).progressArray
This is the only way to share state between activities and services across one application I'm aware of, neither involving sending messages back and forth to keep state consistent (which can be nontrivial task) nor using singletons (which is anti-pattern as you probably know).
But please keep in mind, that need of sharing mutable data in that manner may be a hint, that your design needs an improvement.
If you are expecting the Service to always be started by that Activity, you can send the String data to the Service using the intent which starts the Service. You achieve this using the putExtra and getExtra methods:
Intent serviceIntent = new Intent( this, myService.class );
serviceIntent.putExtra( "pa_0_0", progressArray[0][0] );
serviceIntent.putExtra( "pa_0_1", progressArray[0][1] );
//...
serviceIntent.putExtra( "pa_2_2", progressArray[2][2] );
startService( serviceIntent );
You can get the Strings back out again in your service by overriding its onStartCommand
method, which includes the intent that started the service as a parameter.
progressArrayCopy[0][0] = mIntent.getStringExtra( "pa_0_0" );
Hmm...sounds more like a programming problem rather than Android related. If you have a class and you have an array declaration as in your case above, any new instance of that class won't contain a new instance of that array as well. Say you have a class that is used by some other class and you set its value as class.progressArray[0][0] = ...
and then in some other class you want to access those stored values, you have to have the same instance of that class in order to get the stored values back.
Normally in programming you have different possibilities:
- you pass that created instance containing the data around, i.e. through the constructor or some public setter method or
- you create a static class/static property on that class which will be the same on all instances (caution with that)
- you create a singleton, meaning there will be only one instance throughout the whole life cycle of the application.
精彩评论