I have an activity called ParticleActivity
in my Android Studio
project.
public class ParticleActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "pso.algo.MESSAGE";
private ProgressDialog pd;
private double[] results = {-1.0, -1.0, -1.0};
EditText particles = (EditText) findViewById(R.id.particles);
EditText iterations = (EditText) findViewById(R.id.iterations);
static EditText solution;
public static double userSolution = Double.parseDouble(solution.getText().toString());
static EditText battery;
public static double batteryLevel = Double.parseDouble(battery.getText().toString());
The userSolution
and batteryLevel
are declared so that the user input in those fields can be accessed by another class, customUseCase
.
public class CustomUseCase extends Test {
public ArrayList<Double> costData = MainActivity.costDATA; //costs that the user enters for each resource
public ArrayList<Double> costWlan = MainActivity.costWLAN;
public ArrayList<Double> costUtilities = MainActivity.costUTILITY;
public double batteryCost = ParticleActivity.batteryLevel; //battery cost user enters
public double userSolution = ParticleActivity.userSolution; //user's predicted solution
private int maxIter;
private int noParticles;
I know this isn't a good way of writing code as it says on Android Studio
that I will get a memory leak because the Android
components shouldn't be declared as static variables. But this is the only way (from my little knowledge of Android
) that I can think off where I can access the user input from the EditText
fields in ParticleActivity
so that it can be used in my customUseCase
class. Can someone give me a good way I can write this out? Thank you.
Try using Intent
class and its putExtra
method see the docomentation here https://developer.android.com/reference/android/content/Intent.html
example usage
Intent i = new Intent(ParticleActivity.this,CustomUseCase.class)
i.putExtra("myShow","one");
then on your CustomUseCase class
Intent i=getIntent();
String show=i.getStringExtra("myShow");
I understand that you are trying to pass values to the non-activity for which the intent won't work. Why don't you use shared preference for storing and updating the values.
Setting values in Preference:
// MY_PREFS_NAME - a static String variable like:
public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("userSolution", "test");
editor.putInt("batteryLevel", 12);
editor.commit();
Retrieve data from preference:
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String name = prefs.getString("userSolution", null);//"null" is the default value.
int idName = prefs.getInt("batteryLevel", 0); //0 is the default value.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.