Setting TextView in Another Class - java

I have researched many problems like mine on the internet and none seem to be doing what I'm trying to do. What I am trying to do is get a textview that is currently blank in my high_risk.xml file, and make it have text based on if a button is clicked in another class. Here is what I have so far...
Question13Activity(if the yes button is clicked I want to be able to set the text in the HighRisk Activity)
yes = (Button) findViewById(R.id.finalYes);
yes.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = getIntent();
String text = intent.getStringExtra("New Text");
t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
startActivity(myIntent);
}
});
This is how I have the static variable t defined in my highrisk activity class...
HighRiskActivity(this is where I want the text to be set and displayed)
public static TextView t;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.high_risk);
Intent myIntent = new Intent(HighRiskActivity.this, Question12Activity.class);
myIntent.putExtra("Text", "New Text");
startActivity(myIntent);
Every time I try and access the contents of t in another class and change it's text, it always returns null. Any way I can fix this from happening? Any help would be greatly appreciated :)

You can use Bounds, to get the the data to the new Activity and set the text from there:
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
myIntent.putExtra("<KEY-NAME>", "<TEXT>");
startActivity(myIntent);
And in the Question13Activity.class:
Intent intent = getIntent();
String text= intent.getStringExtra("<KEY-NAME>");
t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);
UPDATE:
Use it so:
HighRiskActivity.class:
yes = (Button) findViewById(R.id.finalYes);
yes.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
myIntent.putExtra("TestKey", "My new Text");
startActivity(myIntent);
}
});
Question13Activity.class:
TextView t;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.high_risk);
Intent intent = getIntent();
String text= intent.getStringExtra("TestKey");
t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);

In order to setText() from another class (for example, from your Custom class), you need to specify the activity on which you'd like to do that.
Unless a class does have a reference of your context, you cannot call 'findViewById()'
To do that, you use Context as a parameter for custom class constructor.
Just after you have set the context will you be able to call
TextView txtView = (TextView) ((Activity)context).findViewById(R.id.text);
txtView.setText("foo");
}

Related

Passing textview value across activities

I am trying to pass a value to a text view into a different activity but only when the button in the activity is clicked.
Here is the activity I am trying to set the text...
public static Button yes;
public static final String TEST_KEY = "test";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question19);
yes = (Button) findViewById(R.id.finalYes);
yes.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
startActivity(myIntent);
Intent i = new Intent(Question12Activity.this, HighRiskActivity.class);
i.putExtra(TEST_KEY, "SOME STRINGSSS");
}
});
Here is the HighRiskActivity that is the destination for updating the textview's value...
TextView t;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.high_risk);
t = (TextView) findViewById(R.id.abusedOrNah);
Bundle extras = getIntent().getExtras();
if(extras != null)
{
String value = extras.getString(TEST_KEY);
t.setText(value);
}
My problem is that no text is printing at the desired activity, am I passing in the wrong data?? Any help would be amazing, this is the last step to the app that I am creating :D
UPDATE
I do not want the Question12Activity to be directed to the HighRiskActivity when the button is clicked. I want it to go to the next activity but still be able to pass the text onto the HighRiskActivity once the button is clicked. Sorry for the confusion, hopefully that makes more sense :)
Try to pass data this way:
Intent i = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString(“test”, "Data you want to pass");
//Add the bundle to the intent
i.putExtras(bundle);
startActivity(i);
Now in your SecondActivity class you retrieve data as below:
Bundle bundle = getIntent().getExtras();
//get the data…
String stuff = bundle.getString(“test”);
I don't know what exactly you are trying to achieve by starting the first activity with the statements below
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
startActivity(myIntent);
But if the Activity you want to start is the HighRiskActivity then the following should fix your issue:
Get rid of the statements related to the Question13Activity mentioned above.
and add the call to start the actual HighRiskActivity like below:
Intent i = new Intent(Question12Activity.this, HighRiskActivity.class);
i.putExtra(TEST_KEY, "SOME STRINGSSS");
startActivity(i); //This line is important to start the new Activity.
So, I guess your major issue here is you didn't start the activity with the startActivity(i); call.
In Activity A you are using the internal intent bundle that is not public to you and private to the intent. In the Activity B you are asking the intent to instead look for a bundle that you provided yourself.
Try something like this in your Activity A
Intent intent = new Intent(context, YourActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("EXTRA1", "string data");
intent.putExtra(TEST_KEY, bundle);
startIntent(intent);
Here is a good writeup on the subject (see answer) Advantages of using Bundle instead of direct Intent putExtra() in Android
You dont need to define two intents in order to connect two Activities. Put the following inside onClick method
Intent i = new Intent(Question12Activity.this, HighRiskActivity.class);
i.putExtra(TEST_KEY, "SOME STRINGSSS");
startActivity(i);

sending text to new activity

i have a relative view with different images each time i click on an image i want a unique text to be sent to another activity. i have been able to achieve this for the first image but i cant get the second image to send the text to next activity.
my code for the first-activity:
b1 = (ImageView) findViewById(R.id.native_amala);
b2 = (ImageView) findViewById(R.id.native_fufu);
b3 = (ImageView) findViewById(R.id.native_jollof);
b4 = (ImageView) findViewById(R.id.native_ofada);
b5 = (ImageView) findViewById(R.id.native_porridge);
b1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
TextView foodName1 = (TextView) findViewById(R.id.amala_text_view);
Intent intent = new Intent(NativeFoods.this,AddToCart.class);
intent.putExtra("Amala",foodName1.getText().toString());
startActivity(intent);
}
});
b2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
TextView foodName1 = (TextView) findViewById(R.id.fufu_text_view);
Intent intent2 = new Intent(NativeFoods.this,AddToCart.class);
intent2.putExtra("fufu",foodName1.getText().toString());
startActivity(intent2);
}
});
}
My second activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_to_cart);
foodView = (TextView) findViewById(R.id.cart_item_text_view);
Intent intent = getIntent();
String str = intent.getStringExtra("Amala");
foodView.setText(str);
intent.putExtra(String key, String value) is the format.
Have a unique key for all putExtra functions and use that key in getStringExtra
You should use Same KEY .
Intent putExtra (String name,
Parcelable[] value)
intent2.putExtra("value",foodName1.getText().toString());
And
Bundle bundle=getIntent().getExtras();
String str=bundle.getString("value");
foodView.setText(str);
You basically need to understand how putExtra works.
You can put some value with keys, for e.g.
intent.putExtra("Amala",foodName1.getText().toString());
Here, Amala is the key and foodName1.getText().toString() is the value.
Then in your next activity you can retrieve the value from key.
i.e. you can do,
String str = intent.getStringExtra("Amala");
In your click listener of b2 ImageView, you are mentioning the key fufu but in your second activity you are getting Amala.
You need to keep common key for all click listeners and then retrieve data from that key in second activity.
You need to keep common key for all click listeners like this way to send data
b1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
TextView foodName1 = (TextView) findViewById(R.id.amala_text_view);
Intent intent = new Intent(NativeFoods.this,AddToCart.class);
intent.putExtra("data",foodName1.getText().toString());
startActivity(intent);
}
});
b2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
TextView foodName1 = (TextView) findViewById(R.id.fufu_text_view);
Intent intent2 = new Intent(NativeFoods.this,AddToCart.class);
intent2.putExtra("data",foodName1.getText().toString());
startActivity(intent2);
}
});
now receiv e data in other activty like this
foodView = (TextView) findViewById(R.id.cart_item_text_view);
Intent intent = getIntent();
String str = intent.getStringExtra("data");
foodView.setText(str);
You using the key is fufu.
intent2.putExtra("fufu",foodName1.getText().toString());
So u get the value then used that key.so change getStringExtra key name change "amala" to "fufu"
foodView = (TextView) findViewById(R.id.cart_item_text_view);
Intent intent = getIntent();
String str = intent.getStringExtra("fufu");
foodView.setText(str);
For Sending Some Text form one Activity to another activity
You can Simply use putExtra() function
putExtra(Sting Key, String value);
Let say you have String variable
String str = "Hello";
After when go to next Next Activity
Intent Intent = new Intent(getApllicationContext(),Your_Activity.class);
intent.putExtra("Hello");
startActivity(intent);
When you want this variable in Your_Activity then simply in onCreate() method
Intent intent = getIntent();
String str = intent.getStringExtra("Hello");
Use this
Intent intent = new Intent(getBaseContext(), NextActivity.class);
intent.putExtra("VALUES", someValues);
startActivity(intent);
Access that intent on next activity
String s = getIntent().getStringExtra("VALUES");

setText to another activity?

I have a button on an activity called "HomePage". When you click the button, I want it to setText to a TextView called "weaponTitle" on a separate activity, called ItemSelection. Though, when I run the setText, it gives the error:
Attempt to invoke virtual method void android.widget.TextView.setText(java.lang.CharSequence) on a null object reference
So this means it can't find the TextView "weaponTitle". Is there a good way to do fix this? I have made sure I set up everything correctly too!
Here is the sliver of code I forgot to share!
new displayPrices(weaponTitle, "Genuine Freedom Staff");
try this
Firstclass
Intent intent = new Intent(getApplicationContext,SecondActivity.class);
intent.putExtra("KEY",value);
intent.putExtra("KEY",VALUE);
startActivity(intent)
Second Activity
Intent intent =getIntent();
confirm_txt =(TextView)findViewById(R.id.txt);
String txt_put =intent.getStringExtra("KEY");
confirm_tId.setText(txt_put);
Inside oncrete of your HomePage Activity
Button yourbutton = (Button) findViewById(R.id.your_button_id);
yourbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Intent intent = new Intent(HomePage.this, ItemSelection.class);
intent.putExtra("weapontitle",value);
startActivity(intent);
}
});
Inside ItemSelection Activity's oncreate
Intent intent =getIntent();
String txt =intent.getStringExtra("weapontitle");
TextView weaponTitle = (TextView) findViewById(R.id.textview_weaponTitle);
weaponTitle.setText(txt);
go through this way,
pass this from your first activity,
Intent i = new Intent(Login.this, ChangePasswordActivity.class);
i.putExtra("savedUser", savedUser);
Log.e("username", "--->>" + savedUser);
startActivity(i);
in second activity , get this as below
Intent extras = getIntent();
savedUser = extras.getStringExtra("savedUser");
Log.e("savedUser", "--->>" + savedUser);
Now you can set text as you got it on other activity.
etUsername.setText(userName);
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("weaponTitle","Genuine Freedom Staff");
startActivity(intent);
In the second activity you do:
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
String weaponName = bundle.getString("weaponTitle");
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(weaponName);

taking variable from another class

I have to spinners, and when I start my app, the PHP only returns values of spinner's first choice.
First code is part of one class (IzboraGrada.java)
public void addListenerOnButton() {
spinner1=(Spinner) findViewById(R.id.spinner1);
spinner2=(Spinner) findViewById(R.id.spinner2);
button=(Button) findViewById(R.id.button);
str_grad=spinner1.getSelectedItem().toString();
str_predmet=spinner2.getSelectedItem().toString();
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent (v.getContext(), MainActivity.class);
url = "http://192.168.1.102/test/spinner.php";
url=url+"?grad="+str_grad+"&predmet="+str_predmet;
i.putExtra("URL",url);
startActivity(i);
}
});
And the second code is part of MainActivity.class that was in intent.
private void initView() {
// show progress dialog
dialog = ProgressDialog.show(this, "", "Loading...");
String url = "http://192.168.1.102/test/spinner.php";
Bundle extras = getIntent().getExtras();
if (extras != null) {
url = extras.getString("URL");
}
FetchDataTask task = new FetchDataTask(this);
task.execute(url);
}
I presume that's because str_grad and str_predmet are not defined in second class. But If I put str_grad and str_predmet in second class, they are can't be resolved as type.Any ideas what to do?
It looks like you are calling this method at the beginning, before an item is selected, so I think the problem is that you are setting the values for str_grad and str_predmet when they are first set so the selected item is the default item. Those are getter functions not listeners.
You need to move those lines inside the onClick() or use onItemSelected() on your Spinners to set those variable values
public void addListenerOnButton() {
spinner1=(Spinner) findViewById(R.id.spinner1);
spinner2=(Spinner) findViewById(R.id.spinner2);
button=(Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
str_grad=spinner1.getSelectedItem().toString(); // move these lines here
str_predmet=spinner2.getSelectedItem().toString();
Intent i=new Intent (v.getContext(), MainActivity.class);
url = "http://192.168.1.102/test/spinner.php";
url=url+"?grad="+str_grad+"&predmet="+str_predmet;
i.putExtra("URL",url);
startActivity(i);
}
});
If I understand your problem correctly, that should solve your problem.

Getting the Context of the enclosing object

New to Java. I am trying to create an explicit Intent to launch send the user to a new tab and pass some data to it. The problem I think I am having is getting the Context of the parent object that I can pass to the Intent Constructor. "This" is not correct. The onCreate code is below. Any pointers to examples would be helpful
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quickactivity);
final Button goButton = (Button) findViewById(R.id.go);
goButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(this, TimeActivity.class);
}
});
}
You can also use:
Intent i = new Intent(v.getContext(), TimeActivity.class);
More info here
Use
<MyClassName>.this
instead. Replacing by the actual class in which this method resides

Categories