Hello guys i have a really dumb problem i have two classes the first class is where the input value needs to be applied/readed and the second is where is inputed i do not know how to do that please someone help me

This is where it is inputed the value

final EditText getMetters = (EditText) findViewById(R.id.getMettersText);
final TextView errors = (TextView) findViewById(R.id.errorMessage);
Button setAlarm = (Button) findViewById(R.id.setAlarmButton);

setAlarm.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        int getM = Integer.parseInt(getMetters.getText().toString());
        if (getM > 3000) {
            errors.setText("ERROR");
        } else {
            errors.setText("ACCEPTED");
        }
    }
});

And here where needs to be writen

circle = mMap.addCircle(new CircleOptions()
    .center(latLng)
    .radius(1000) // here needs to apply the value
    .strokeColor(Color.RED)
    .fillColor(Color.GREEN)
);

Recommended Answers

All 12 Replies

You'll need an instance of the one class in the other (or some other relation through which they can pass data) and pass it as a parameter to a method, or use a getter.

Hmm i dont get it i have tryed the following code but i have another problem here... i cannot use sa.apply() in the .radius() method

This is SettingsActivity.java

public int metters;

public void setRadius(int radiusss) {
    metters = radiusss;
}

public int getRadius() {
    return metters;
}

public void apply() {
    setAlarm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int getM = Integer.parseInt(getMetters.getText().toString());
            if (getM > 3000) {
                errors.setText("ERROR");
            } else {
                errors.setText("ACCEPTED");
            }
        }
    });
}

Here is the MainActivity.java

SettingsActivity sa = new SettingsActivity();
        int metters;
        sa.setRadius(metters);
        // Creating circle around marker
        circle = mMap.addCircle(new CircleOptions()
            .center(latLng)
            .radius(sa.apply()) // Not alowed like this
            .strokeColor(Color.RED)
            .fillColor(Color.GREEN)
        );

what does the error message say?

Error:'void' type not allowed here

EDIT: the default radius i want to set is on 500 metters, is there any way that to be changed after circle is created ?

All that apply() does is to set a listener. For the radius method you need an actual numeric value, which apply() does not supply.
I'm curious about this mistake. What did you hope would happen if your code was able to execute the way you intended?

Woow that was really big embarrassing :D but still i dont get it.. i have changed again with return value into apply but now application crashes if you are avuncular to show me in example how can be done it would be awesome for me and my eyes :P

You declare metters in here but you don't set it to anything. Then you pass it as a parameter to setRadius. Basically you pass nothing in here? I think this is where James points out that you need a numeric value

SettingsActivity sa = new SettingsActivity();
        int metters;
        sa.setRadius(metters);
        // Creating circle around marker
        circle = mMap.addCircle(new CircleOptions()
            .center(latLng)
            .radius(sa.apply()) // Not alowed like this
            .strokeColor(Color.RED)
            .fillColor(Color.GREEN)
        );

apply() just sets a listener. How can it possibly return a value for radius?
You don't have a value for radius until after the onClick is executed, and the onClick can't be called until after apply() has executed..

Without the whole context it's hard to give a relevant example, and the wrong example will just cause more confusion.

Here's what real programmers do when faced with a challengle like this: they sit down and pretend to be the JVM stepping through the application code, keeping track of the variables etc. Once you have worked out what you would need if you were the virtual machine then you will know what Java needs.
(It's called the "mental model" - all competent programmers have a mental model of what the computer is doing with their code. Without a mental model all you can do is try stuff at random and hope for the best.)

Judging by your code it looks like you are doing Android development and trying to pass data from one activity to another. Due to the nature of Android activities (multithreaded, and can be GC'ed at any moment if, e.g. you get a phone call) you can't deal with passing data between them any of the normal java ways.

This leaves you with a few options which are easily googled for as "Android pass data between activities" or similar.

The following is a summary of the most common techniques.

Option 1: Application-Global values

This is useful if most/all of your activities will require access to the data. To implement it create a class that extends 'Application' and add the class name to the manifest under the <Application> tag. Inside this class you can have public static variables which can be accessed from any activity using getApplicationContext(). Here is an example:

class GlobalApplication extends Application {
    public static int value;
}

class SettingsActivity extends AppCompatActivity { 
    @Override
    protected static void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GlobalApplication me = (GlobalApplication)getApplicationContext();

        me.value = 1;
    }
}

class MainActivity extends AppCompatActivity {
    @Override
    protected static void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GlobalApplication me = (GlobalApplication)getApplicationContext();

        Log.d("Global value is: " + me.value);
    }
}
Option 2: Passing data through intents

If your first activity creates the second via an intent, you can add data to the intent to move values. This is preferred over creating an Application if it can be done. Here is an example:

class SettingsActivity extends AppCompatActivity {
    @public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int value = 1;
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("value",value);
        startActivity(intent); // silly, since it will immediately hide this activity, but this is just an example
    }
}

class MainActivity extends AppCompatActivity {
    @public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int value;
        Intent intent = getIntent();
        if (intent.getExtras() != null) //check if we even have extras
            value = intent.getIntExtra("value",-1);//2nd parameter is default value
    }
}
Option 3: Passing data through settings

The third option is to pass data via persistent settings. These are also global (like in Option 1) but they also persist after your application dies. This means that even if you exit your app and start it again, these settings will still be saved. Here is an example:

class SettingsActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int value = 1;
        // We will call this preferences file "settings"
        getSharedPreferences("settings", Context.MODE_PRIVATE).edit().putInt("value",1).apply();
    }
}

class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int value = getSharedPreferences("settings", Context.MODE_PRIVATE).getInt("value",-1);
    }
}

One of those methods should work for you. If not you may find others on google. Also if you are not, in fact, doing Android development please forgive my assumption and disregard this response.

PS: @JamesCharrill If I am right and this is Android development then the two classes in question will be running concurrently with a hidden manager passing data between them. Not as easy to 'think through' the JVM when the JVM is dalvik (or ART) and is 'maybe' multithreading your applications and maybe not.

commented: Great contribution and clarifies many issues. Thanks. +15

Thanks man i think i need the third one because the app needs to update on every user location change and also show notification. I will come with more info tommorow when ill test the code. Hope it will do well.
Also i have question abouth the third one, it is possible after the finished task to start a new session ?

Well the other options can be [startActivityForResult()](http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)) which would be my preffered choice given that you have some field and you want user to change value/s and then you will lsiten for result [onActivityResult()](http://developer.android.com/reference/android/app/Activity.html#onActivityResult(int, int, android.content.Intent))

I tried the intent and it works good but theres one problem every time i click the button it starts new activity and saves it but doesn't close it/finish... also i would like to ask how i can use the third method i tried it but dont know how to use it and does it run in background after the application is closed ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.