I have two Java files
The first one extends AppCompactActivity
And the second one extends SQLiteDataBaseHelper
So this is the question:
Can I access an EditText variable from first one to second one?
of course, you can and Edit Text at the end is a place to store data so you can create an instant of that class you want to access and then you can use setters to set data or getters to get it, check this:
public class MainActivity extends AppCompatActivity {
private EditText editText ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public EditText getEditText(){
return editText ;
}
public void setEditText(EditText editText){
this.editText = editText;
}
here is the database class :
public class access extends SQLiteOpenHelper {
private MainActivity mainActivity= new MainActivity() ;
public void setData(EditText editText){
mainActivity.setEditText(editText);
}
Related
is it possible to get all activities in the application? i have a global integer variable that should be in the ActionBar of every activity. i thought something like this:
for (Layout/Activity l in (all activites)) {
l.setTitle(variable);
}
i already tried it with R.layout but this didnt work for me.
How can i do this or is there a better way to display my variable in all activity labels? later i want to call this code from my set method for the global variable.
There is only one activity running at a time, so you can’t get this kind of references.
Said that, I think the way to go it’s create an int static variable in some class, and called it from your activities.
//SomeClass
public static int xValue = 0;
//ActivityOne || ActivityTwo || ActivityThree ...
String text = String.valueOf(SomeClass.xValue);
SomeClass.xValue = 1;
Because it’s a public static variable, you don’t need to instantiate any object to get/set its value, and it will be accesible from any class. Furthermore, this value will be reachable as long as its class is in the memory, and destroy just when class gets unloaded.
yes it's possible with singleton.
This is how to use singleton:
This is Singleton class:
public class Singleton {
private static Singleton mInstance = null;
private String mTitle;
public void setmTitle(String mtitle){
this.mTitle=mtitle
}
public String getmTitle(){
return mTitle;
}
public static FilterArrayList getInstance(){
if(mInstance == null)
{
mInstance = new FilterArrayList();
}
return mInstance;
}
}
This is the first activity:
public class FirstActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Singleton.mInstance.setmTitle("This is Singleton");
}
}
and in second activity:
public class SecondActivity extends Activity {
String Title;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Title=Singleton.mInstance.getmTitle();
}
}
Hi I have an activity named BaseActivity, which extends Activity.
from this i have to go to SettingsActivity which extends PreferenceActivity, on menu button press. To start a AsyncTask, which is in an independent class, i need an instance of BaseActivity. How can i get a BaseActivity instance in the SettingsActivity?
is there any way like,
eg:
intent.putExtra("activity_instance",BaseActivity.this);
Use getters and setters and make the class they reside as singleton class.
This is a singleton class.Using this class we can share data(ex: int,boolean,activity instance ...etc) all over the class.
public class CommonModelClass
{
public static CommonModelClass singletonObject;
/** A private Constructor prevents any other class from instantiating. */
private Activity baseActivity;
public CommonModelClass()
{
// Optional Code
}
public static synchronized CommonModelClass getSingletonObject()
{
if (singletonObject == null)
{
singletonObject = new CommonModelClass();
}
return singletonObject;
}
/**
* used to clear CommonModelClass(SingletonClass) Memory
*/
public void clear()
{
singletonObject = null;
}
public Object clone() throws CloneNotSupportedException
{
throw new CloneNotSupportedException();
}
//getters and setters starts from here.it is used to set and get a value
public Activity getbaseActivity()
{
return baseActivity;
}
public void setbaseActivity(Activity baseActivity)
{
this.baseActivity = baseActivity;
}
}
In BaseActivity class do like this.
Class BaseActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
CommonModelClass commonModelClass = CommonModelClass.getSingletonObject();
commonModelClass.setbaseActivity(BaseActivity.this);
//after using the BaseActivity instance dont forget to call commonModelClass.clear(); else it wont be garbage collected
}
}
In SettingsActivity do like this
Class SettingsActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
CommonModelClass commonModelClass = CommonModelClass.getSingletonObject();
Activity instanceBaseActivity= commonModelClass.getbaseActivity();;
}
}
please give tick if this works
You are confusing activities with class objects. The moment activity class is instantiated it obeys all activity life cycle rules, importantly system can kill this activity any time. So you have to design activities in such a way that it shouldn't be dependent on another activity instance at all but only drive the results. you can write a helper class and call it again and again if you want. if not use storages like sdcard or preference or sandbox to store the information and retrieve it from the other activity. If you want to keep some of these information in memory then subclass Application class and keep them at the application level.
Make a static Context in "Base Activity"
public class BaseActivity extends Activity{
public static Context ctxt;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctxt = BaseActivity.this
}
}
and in your "PreferenceActivity" activity use this way
BaseActivity.ctxt
i have the class MainActivity
public class MainActivity extends Activity {
int mVariable= ((Constant)getApplicationContext()).variable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button count= (Button)findViewById(R.id.button1);
final TextView tx = (TextView)findViewById(R.id.text);
count.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
mVariable ++;
tx.setText(" "+mVariable);
....
and the Constant class
public class Constant extends Application {
public int variable=0;
}
and this doesn't work ..i declare Constant in Manifest.
<application
...
android:name=".Constant"/>
I want to set value by onClick like this code and change the value of a global variable.. thanks
You can make this variable as Static and access it in every other classes in your project like Constant.variable :
public class Constant extends Application {
public static int variable = 0;
}
And
public class MainActivity extends Activity {
......
tx.setText(" " + Constant.variable);
.......
}
And now the variable is available throughout the whole project by Constant.variable and when you change the variable's value, it will be applied to all classes.
I don't now what actually happened inyour project but above is just a suggestion to your problem.
I am working in android. I have two acitivities in my project. I have declared a public static variable in one activity like this:-
public static String name="KUNTAL";
In my second activity i am trying to use this variable, then it is generating error that this name variable is not exist.
Is this possible to use a variable anywhere in my project if it is declared as public ?
Please suggest me what mistake i have done.?
Thank you in advance...
public class Activity1 extends Activity {
public static String name="KUNTAL"; //declare static variable.
#Override
public void onCreate(Bundle savedInstanceState) {
}
}
public class Activity2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
Activity1.name; //way to access static variable using dot operator.
}
}
I think you must access them in a 'static way', i.e.:
String myVar= name; // wrong
String myVar= TheClassThatContainsName.name; // right
You can use the variable specified as public static in any Activity but you need to access that variable by using the Activity name where you Declared it.
For Accessing in Second Activity just use ;
Activity1.name ="Me";
means that name variable belongs to Activity1 and you are using in Acvity2
Declaring variables public static is not the recommended way of passing values between two activities. Use Intent object instead:
public class Activity1 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
yourButton.setOnClicklistener(listener);
}
}
//On click of some button start Activity2:
View.onClicklistener listener = new View.OnClickListener(){
#Override
public void onClick(View v) {
Intent mIntent = new Intent(Activity1.this,Activity2.class);
mIntent.putExtra("yourKey","yourValue");
startActivity(mIntent);
}
};
//and then in your activity2 get the value:
public class Activity2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
String yourValue = getIntent().getExtras().getString("yourKey");
}
}
if you're using the same variable in more than one activity then make a class something like ActivityConsts{}, and declare and initialize this variable in there(in ActivityConsts). And access this variable from anywhere with the class name.
ex-
declare a class-
public class ActivityConsts {
//your String var
public static String name="KUNTAL";
}
now in your activity:
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
String yourStringVar = ActivityConsts.name;
}
}
There is no datatype specified for your variable. Use
public static String name="KUNTAL";
I have an activity which creates an object instance of my class:
file MyActivity.java:
public class MyActivity extends Activity {
TextView myView = (TextView)findViewById(R.id.myView);
...
Points myPoints new Points();
...
}
--------------------------------------------------------------
file Points.java:
private class Points {
...
HOW TO USE myView HERE ???
...
}
--------------------------------------------------------------
How do I use the UI objects in my class (which does not extend an
Activity)? Should I pass some context to my Points class? How do I do, exactly?
see you post, i've edited it , to fix the problem
hope it helps :=)
here is the Edit :
file MyActivity.java:
public class MyActivity extends Activity {
TextView myView ;
protected void onCreate(android.os.Bundle savedInstanceState) {
myView = (TextView)findViewById(R.id.myView);
Points myPoints = new Points(this);
myPoints.displayMsg("Hello World !!!");
}
}
--------------------------------------------------------------
file Points.java:
private class Points {
protected MyActivity context;
//add a constructor with the Context of your activity
public Points(MyActivity _context){
context = _context;
}
public void displayMsg( final String msg){
context.runOnUiThread(new Runnable() {
#Override
public void run() {
context.myView.setText(msg);
}
});
}
}
Your Points can't be a private class without being an inner class. So your code doesn't even compile...
Pass the view as parameter to the constructor of your Points class:
public class MyActivity extends Activity {
TextView myView = (TextView)findViewById(R.id.myView);
Points myPoints new Points(myView);
private class Points {
public Points(TextView view) {
// todo
}
}
}
You should do everything and pass back the value to the activity to handle UI instead of doing any UI related stuff in the point stuff.
You can pass the main Activity's context (using Points(getApplicationContext());) to the class as a constructor parameter. You could also pass the specific UI elements you want to manipulate.
A better way to do it, however, may be to have Points not know about the Activity. Have your Activity call Points methods and take the necessary actions based on the method output.
You could just pass the view to your class.
Points myPoints = new Points(myView);
private class Points
{
private TextView mTextView;
Points(TextView textView)
{
this.mTextView = textView;
}
}
i was in same trouble..
i found the simple way..
make a static variable and function ...
call from other class..
TestActivity.java
public class TestActivity extends Activity {
static EditText edit_text1;
public void onCreate(Bundle savedInstanceState)
{
.....
edit_text1 = (EditText) findViewById(R.id.edit_text1);
.....
}
public static void setMSG(String str)
{
edit_text1.setText(str);
}
}
Test2.java
TestActivity.setMSG("this is text");
Could work using an interface
file MyActivity.java:
public class MyActivity extends Activity implements Points.MyListener {
TextView myView;
... onCreate(...){
myView = (TextView)findViewById(R.id.myView);
Points myPoints = new Points();
//pass in MyActivity's instance of the listener
myPoints.addListener(this);
}
#Override
public void updateTextView(String message){
myView.setMessage(message);
}
}
file Points.java:
public class Points {
public Points(){
}
public interface MyListener{
void updateTextView(String message);
}
MyListener myListener;
public void addListener(MyListener listener){
myListener = listener;
}
public void updatePoints(){
//do some operations in calculatePoints()
String points = calculatePoints();
//update views using MyActivity's implementation of updateTextView()
myListener.updateTextView(points);
}
}
Doing it this way, events can be fired / messages sent, for lack of better terms, from the external class to update the Activity UI. This might be overkill if all sb need is to call a method in the Points class that returns something