Can't update TextView from another activity - java

I have an activity with a TextView that needs to be updated from a second activity.
I can pass the Text view data to the 2nd activity ok, but when I try to update that TextView
within the 2nd activity it crashes. My code:
1st Activity (where the TextView is defined in my xml):
Intent intent = new Intent(1stactivity.this, 2ndactivity.class);
intent.putExtra("homescore_value", ((TextView)findViewById(R.id.score_home)).getText());
startActivity(intent);
// code snippet
Then in my 2nd activity:
Bundle bundle = getIntent().getExtras();
hometext = bundle.getString("homescore_value"); // this works and displays the correct String from 1st activity,
but it crashes when I try to pull in as a TextView:
// other code snipped
int homescore = 0;
String Home_nickname = "HOME ";
TextView homestext = (TextView) bundle.get("homescore_value");
hometext.setText(Home_nickname +": "+homescore );
Please help.

You are trying to get a String as a TextView (you are setting a String in the intent from the first Activity).

You trying to cast String to TextView. The code that crashes is equivalent of:
String text = bundle.get("homescore_value"); //this is ok
TextView textView = (TextView)text; //this is bad
You should do instead:
String text = bundle.get("homescore_value");
TextView textView = (TextView)findViewById(R.id.yourTextViewId);
textView.setText(text);

This line here:
intent.putExtra("homescore_value", ((TextView)findViewById(R.id.score_home)).getText());
is attaching a String along with the intent, not a TextView.
You must inflate a new TextView within the 2nd activity, by either declaring it in the layout.xml, or programmatically placing it within the layout.

Something that solved part of this problem for me was setting the receiving String variables to null like this:
public String param1new = null;
public String param2new= null;
My issue with this is I'm trying to set background colors on several TextViews and only the first one is being set at this time.

Related

setting textview to the string passed from another activity

I have an edittext in first activity (apples.java). User can write anything using edittext which is passed as a string to second activity (Bacon.java) on clicking a button. Second activity has a textview written Bacon. When a string is passed from first activity, Bacon is replaced with the passed string/text.
What is happening : When nothing is written using editText and button is clicked to go to second activity, Bacon disappears.
My requirment is that if nothing is passed using EditText and only button is clicked, Bacon should not get replaced.If something is passed, it should replace Bacon.
apples.java:
Intent i = new Intent(apples.this,Bacon.class);
final EditText applesInput = (EditText) findViewById(R.id.applesInput);
String userMessage = applesInput.getText().toString();
i.putExtra("applesMessage",userMessage);
startActivity(i);
Bacon.java
final TextView baconText = (TextView) findViewById(R.id.baconText);
Bundle applesData = getIntent().getExtras();
if(applesData==null){
Toast.makeText(Bacon.this, "Love u", Toast.LENGTH_SHORT).show();
return;
}
String applesMessage = applesData.getString("applesMessage");
baconText.setText(applesMessage);
This is first activity
This is second activity
Do this:
if (!TextUtils.isEmpty(applesMessage))
baconText.setText(applesMessage);
In apples.java write if(applesInput.getText().toString().equals(""); { i.putExtra("applesMessage",userMessage);} just add this..
your problem is that Bundle is not null, so the condition is never satisfied.no string is set does not mean that your bundle is Null
change it to
final TextView baconText = (TextView) findViewById(R.id.baconText);
Bundle applesData = getIntent().getExtras();
if(applesData!=null){
String applesMessage =applesData.getStrng("applesMessage")
if(applesMessage ==null ||applesMessage .trim().equals("")){ //trim to delete spaces
Toast.makeText(Bacon.this, "Love u", Toast.LENGTH_SHORT)
.show();
return;
}
baconText.setText(applesMessage);
}
}

Editing Bundle extras and saving it

So I have a situation where I populate TextView from Intent extras and then allow users to edit it. When user taps on edit Button, EditText appears with text shown in TextView.
Then user can input new value.
And finally when the save Button is pressed, new value isn't saving, instead the old value is still shown.
Here's my code:
Bundle extras = getIntent().getExtras();
if (extras != null){
textViewNazivTodoaDetails.setText(extras.getString("naslov"));
textViewDatumTodoaDetails.setText(extras.getString("datum"));
final int id = extras.getInt("id");
String oldText = extras.getString("naslov");
editTextDetaljnoIspravljanje.setText(oldText);
}
final String newText = editTextDetaljnoIspravljanje.getText().toString();
buttonDetaljnoIspravljanjeDone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textViewNazivTodoaDetails.setText(newText);
}
});
}
What am I doing wrong?
You are clearly setting the text back to old text inside your onClick with the following line.
textViewNazivTodoaDetails.setText(newText);
You don't need this line inside onClick. Instead of this you should have the code to save the new value to where ever you want to save it.
The line of code where you initialized newText was called before you edited it and contains the same value as oldText. Hence you see the old text again.
Your text view already has the newly entered text.

Android ListView - onItemClickListener

I'm beginning in mobile application development and I want to know how to send an item from a ListView to appear in another activity; Then I can show more information about each item.
I managed to display the title and description with the following code:
MainActivity
TextView txt = (TextView)findViewById(R.id.textView1);
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("title", txt.getText());
startActivity(intent);
SecondActivity
TextView txt = (TextView)findViewById(R.id.show_textView1);
Bundle b = this.getIntent().getExtras();
String val = b.getString("name");
txt.setText(val);
but how do I set putExtra for an image to display in the next Activity ?
P.S. everything is working fine, the xml file where the item details will be shown is set.
What is the source of the image?
If a file, pass in the String to the file location.
bundle.putString("file-loc",fileLocation);
If a resource, pass in the resource.
bundle.putInt("resource-id",resid);
If it's just a bitmap, you can actually pass it directly, as a Parcelable.
bundle.putParcelable("bitmap",bitmap);
You can send the ImageView's id (R.drawable.your_image) and then fetch it and show it in the ImageView of other activity

Transfer textview data between activities?

I'm currently making an app that has edittext and 2 buttons, everytime I write something in to edittext and then press button1, a new textview is added. This is the code:
public void novVnos (View v){
EditText eText = (EditText) findViewById(R.id.editText1);
LinearLayout mLayout = (LinearLayout) findViewById(R.id.linearLayout);
mLayout.addView(createNewTextView(eText.getText().toString()));
}
private TextView createNewTextView (String text){
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView newTextView = new TextView(this);
newTextView.setLayoutParams(lparams);
newTextView.setText(text);
return newTextView;
}
Basically what I'm doing is adding new "players" and once I've added them all I want to press button2. Button2 opens a new activity and the new activity should read all the textviews that I've added in the previous activity.
What I would do is create a "Player" class and make a static ArrayList<String> players inside your Player.java class. Every time you call createNewTextView(textView) add whatever variable text is to the players ArrayList.
In your next Activity you just call Player.players.get(index) or whatever ArrayList function you need to do whatever work you want with it in the next class. You also could create this ArrayList in your current Activity and pass it as an extra in the Intent but I think creating a separate class for the players would be easiest.
Your ArrayList obviously doesn't need to hold Strings. It could hold whatever you want including a Player object.
ArrayList Docs
this is an example :
in the first activity :
Button search = (Button) findViewById(R.id.Button02);
search.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, result.class);
Bundle b = new Bundle();
EditText searchtext = (EditText) findViewById(R.id.searchtext);
b.putString("searchtext", searchtext.getText().toString());
//Add the set of extended data to the intent and start it
intent.putExtras(b);
startActivityForResult(intent,RESULT_ACTIVITY);
}
});
and in the second activity at onCreat :
Bundle b = getIntent().getExtras();
String searchtext = b.getString("searchtext"); //here you get data then use it as you want
//here I use it to show data in another edittext
EditText etSearch = (EditText) findViewById(R.id.searchtext2);
etSearch.setText(searchtext);
Implementing a "Player" class will be a way to go. In that regard, you don't have to worry about transferring data between two activities. You can use Singleton method to make sure your class has been defined only once. You need to make sure it gets defined only once because let's say if you user closes application or kills app, then when he opens app again, it would create another class, so all of your data will be lost.

pass 2 values from one to another activity

i am very new to android.
i m trying to develop a login form for practise , and i m unable to pass and display 2 values
to another activity
Intent intent = new Intent(this, Success.class);
EditText edituser = (EditText) findViewById(R.id.userinput);
EditText editpass= (EditText) findViewById(R.id.passinput);
String username = edituser.getText().toString();
String password = editpass.getText().toString();
intent.putExtra(EXTRA_USER, username);
intent.putExtra(EXTRA_PASSWORD, password);
startActivity(intent);
and target activity is
Intent intent = getIntent();
String username = intent.getStringExtra(MainActivity.EXTRA_USER);
String password = intent.getStringExtra(MainActivity.EXTRA_PASSWORD);
// Create the text view
TextView userView = new TextView(this);
TextView passView = new TextView(this);
userView.setTextSize(40);
userView.setText(username);
passView.setTextSize(40);
passView.setText(password);
// Set the text view as the activity layout
setContentView(userView);
setContentView(passView);
Your problem is that when you call setContentView() the second time, you remove the older view from the screen.
Try adding your Views to a ViewGroup like a LinearLayout and then adding that to the screen instead of individual Views.
Something like:
LinearLayout ll = new LinearLayout(this);
ll.addView(userView);
ll.addView(passView);
setContentView(ll);
You are getting the second value only, because you are setting your view twice..
the second one overrides the username..
You can use addContentView instead, or create an xml that contains 2 text views and use that..
use another layout and set intent values in the second layout....
setContentView(R.layout.anotherlayout);
TextView user = (TextView) findViewById(R.id.usertext);
TextView pass= (TextView) findViewById(R.id.passtext);
Intent intent = getIntent();
String username = intent.getStringExtra(MainActivity.EXTRA_USER);
String password = intent.getStringExtra(MainActivity.EXTRA_PASSWORD);
user.setText(username);
pass.setText(password);

Categories

Resources