0

i have this code but it doesnt work (Force Close) ,i use this in C# but its not working in java

ClassA c = new ClassA(); c.TextView1.setText("test");

i need to set the text view from ClassB can it be done without using the Intent because the Intent need to start the Activity all over and all the data will be lost

can anyone suggest a code for this

also can i set the int x value in ClassA from ClassB

Medo
  • 45
  • 3
  • 11

7 Answers7

2

Yes, you can do -

Intent i = new Intent(classA.this, classB.class);
Bundle bundle = new Bundle();
bundle.putExtra("name For Identification", "Value);
i.putExtras(bundle);
startActivity(i);

In your second class, i mean Class B

Bundle bundle = getIntent().getExtras("name for Identification");
String text = bundle.getString("name For Identification");

Simply set this text to your TextView And, the Class B also should extends the Activity Otherwise, the getIntent() code will not work.

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
0

Within ClassA

Define your TextView

private TextView1 txtView1;

Load it during onCreate

txtView1 = (TextView) findViewById(R.id.txtView1);

Write a setter

public void setTextOnTextView1(String text) {
  txtView1.setText(text);
}

In your other class you can now

c.setTextOnTextView1("Hello");
Rawkode
  • 21,990
  • 5
  • 38
  • 45
0

Try this

add this in activity 1

Intent myIntent = new Intent(Activity1.this, Activity2.class);
       myIntent.putExtra("UserId",UserId);
       myIntent.putExtra("UserName",UserName);
       startActivity(myIntent);

add this in activity 2

Intent intent = getIntent();
UserId=intent.getStringExtra("UserId");
UserName=intent.getStringExtra("UserName");
KMI
  • 496
  • 4
  • 24
0

Yup it can be done without using intent, use static methods/members of class

Get the TextView object from a static method from ClassA and similarly define a static method setX(int x) method in ClassA

So for example

class ClassA{

static TextView tv; //this should be intialized in your code via findViewByID or by code depneds

static int x;


static public TextView getTextView(){
return tv;
}


static public void setX(int xP){
x = xP;
}

}

from ClassB you can invoke ClassA.getTextView() and ClassB.setX(12)

user_CC
  • 4,686
  • 3
  • 20
  • 15
0

intent to class B from class A

Intent toClassB = new Intent(classA.this,classB.class);
toClassB.putExtra("StringId","value"); 
startActivity(toClassB);

//get value
Intent intent = getIntent();
String getValue = intent.getStringExtra("StringId");
//set text
textView.setText(getValue);

hope this helps

Hassy31
  • 2,793
  • 3
  • 21
  • 37
0

To Pass value between two activites you can use shared Preferenceas follow

In Activity A:

public class activityA extends Activity{

private  final String MY_value = "value";//variable used for shared preference
@Override
    public void onCreate(Bundle savedInstanceState)
    {
SharedPreferences myPrefs = getBaseContext().getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
                    SharedPreferences.Editor prefsEditor = myPrefs.edit();
                    prefsEditor.putString(MY_value, "xyz");
                    prefsEditor.commit();
}
}

In Activity B you can retrive that value as follow:

public class activityB extends Activity{

    private  final String MY_value = "value";//variable used for shared preference
    @Override
        public void onCreate(Bundle savedInstanceState)
        {
    SharedPreferences myPrefs1 = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
                roadworthynumber = myPrefs1.getString(MY_value, "value");

    }
    }
Parth Dani
  • 535
  • 4
  • 19
0

Create a function returnThis() in Class A

ClassA returnThis()
{
  return this;
}

In classB call this function and by using returned reference set textView of classA

Raheel
  • 4,953
  • 4
  • 34
  • 40