Spinner choice to be used across multiple activities - android

Having tried several of the methods suggested by other posts on this site I can only assume I'm thick, so if someone can have a glance over my code I would be grateful.
I have the selected item in a spinner in one activity and stored that item as a string, then passed the string to the next activity and displayed that in a
Text View in that activity. That works a treat.
What I would like to do now is pass that string on to a third activity and display it in another Text View in the third activity.
To achieve this here is the code from my first activity after a button click.
Intent recordissues2 = new Intent(RecordIssue.this, RecordIssue2.class);
Bundle sitename = new Bundle();
sitename.putString("txt1a",spinnerbuilding.getSelectedItem().toString());
recordissues2.putExtras(sitename);
startActivity(recordissues2);
And for the second activity in onCreate
Bundle sitename = this.getIntent().getExtras();
String txt1a = sitename.getString("txt1a");
((TextView) findViewById(Sitelbl2)).setText(txt1a);
if you can help me with some example code to achieve the next step and where it should go and preferably exactly how it is working that would be perfect.
Thank you in advance.
Rick

you can use shared preferences to store data and use them throughout the application in any activity class:
SharedPreferences myprefs= this.getSharedPreferences("shared_key", MODE_WORLD_READABLE);
myprefs.edit().putString("spinner_value", value).commit();
You can retrieve this info across your app like this:
SharedPreferences myprefs= getSharedPreferences("shared_key", MODE_WORLD_READABLE);
String spinner_value= myprefs.getString("spinner_value", null);

In your RecordIssue2 activity, you have already stored your text as txt1a. Using the same concept, add it to your Intent's bundle when navigating to the third activity.
Intent recordissues3 = new Intent(RecordIssue2.this, RecordIssue3.class);
Bundle sitename = new Bundle();
sitename.putString("txt1a",txt1a);
recordissues3.putExtras(sitename);
startActivity(recordissues3);

Related

How would I display edittext field dynamically

I have a string array and I have to display the string array in a edittext box showing one value at a time.
For Instance,
String str = new String{"abc", "def", "ghi"};
I have to show these values in a edittext box such that the value keep changes periodically as soon as a user opens this activity.
Please help me out, thanks in advance.
You can put this string array into parent activity of this activity. And wen you are starting intent of this activity(which contains edittext), put extra values i,e in your case array[i] and then start the activity.
starting intent from parent activity :
Intent i = new Intent(FirstScreen.this, SecondScreen.class);
i.putExtra("STRING_you_NEED", string[i]);
and then get it on your activity that contains editText !
Bundle extras = getIntent().getExtras();
edittext.setText(extras.getString("STRING_you_NEED"))
There are lots of ways to store an index to the string array and retrieve it whenever you want. Depending on the use case, you can try the following,
Use SharedPreferences and use a key-value pair to store the index and retrieve it when you want to show the string in the Activity(Persistent)
Extend your Application(or create a static class) and use a static int variable to store the index(Non-Persistent)

How can I access a string defined in a .java file in the corresponding .xml file?

in myapp.java, I define a string based off user input from another activity. How can I access that string in activity_myapp.xml and show it in a TextView? I've tried android:text=myapp.stringname, but it doesn't seem to work.
Edit: The problem is not that I'm unable to get the text into the .java file, but I'm unable to get it into the corresponding .xml file.
well you can do this in java. as you have got the value from previous activity just put it in the current activity TextView.
TextView example = (TextView)findViewById(R.id.yourtextview);
example.setText("yourstring");
The way I suggest you do is put that text as an extra in the intent. In the other activity find the text view by id that you want to populate the data with. Then set the text based on that Extra into from the Intent.
String val = editText.getText();
Intent i = new Intent(this, OtherActivity.class);
i.putExtra("some_string",val);
startActivity(i);
And in the other Activity, onResume you can do the following
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("some_string");
}
newEditText.setText(value);
Hope this helps.

Dynamically create pages/activity in Android?

Okay, so i just created an activity that queries post titles from a database and post it on a view using an adapter.
I would like to implement the following:
Once I click the title, I should be taken to a separate page/activity that displays the content section of that title. Similar to a blog app that goes to a blog post when we click on the title.
I'm going through a block right now. How does one get this done?
In Intent you can use add extra e.g id from each:
First: and in activity you can change it Dynamically setContentView(xxx);
xxx: change depend on extra you add previously
Second: you can create standard layout and depend on extra you can fill
e.g id==1 ===> fill textview to "zzzz"
id==2 ===> fill textview to "mmmm"
... and so on
Well actually you're not dynamically creating an activity. The content of your second (detail) activity just depends on the user's input on the first activity.
As soon as the user click's on a title on your first activity, you create a new intent and append the post's to the intent's extras.
When you startup your second activity using this intent, you will be able to get the post's id from the extras and load the content from your database.
FirstActivity
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtra("POST_ID", <post_id>);
startActivity(i);
SecondActivity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Bundle bundle = getIntent().getExtras();
String postId;
if(bundle != null) {
// process your id.
postId = bundle.getString("POST_ID");
}
}

How to recall the username previously entered in another activity

I am passing a value for username in one activity through XML. I want to recall the value in the next screen, like, 'Hello/Welcome, '. How is it possible using GetExtra and PutExtra?
Pls. find the below code, which I have tried for the same.
For storing the value of username in one activity:
Intent myActivity2 = new Intent(this,Activity1.class);
myActivity2.putExtra("nickname", R.id.mynickname3);
startActivity(myActivity2);
For recalling the value in other activity:
Bundle extras = getIntent().getExtras();
String val = extras.getString("nickname");
TextView tv=new TextView(this);
tv.setText(val);
If you want to reuse a username, use sharedPreference.
It is not a good idea to pass a userName to another activity then use that.Field like userName may need to use again and again.
In the first activity, you are passing in an integer value (R.id.mynickname3). Instead you need to give it a String. Use this instead:
myActivity2.putExtra("nickname", getString(R.id.mynickname3));
As a side note, you are trying to get a Bundle unnecessarily. Try this:
String val = getIntent().getStringExtra("nickname");
TextView tv = new TextView(this);
if (val != null)
{
tv.setText(val);
}
You're nearly right.
It is not enough to just pass the id of the EditText. You have to use below code -
((EditText)findViewById(R.id.mynickname3)).getText().toString();
int putExtra. Everything else should be fine.
UPDATE: Don't forget to set your TextView as contentView in OnCreate via:
setContentView(..)
Just a silly mistake in your code:
myActivity2.putExtra("nickname", R.id.mynickname3);
Where you have written ID of the EditText (nickname), instead it should be as a:
txtNickName = (EditText) findViewById (R.id.mynickname3);
myActivity2.putExtra("nickname", txtNickName.getText().toString());

how to display the spinner selected values to text view in another activity

I have two activities namely activity1 and activity2.
In my first activity I have a spinner and in my second activity I have a text view(txt1).
Here, all I want is that when a spinner value is selected, the selected value of that spinner should be displayed in the text view in second activity.
I can display the selected value of the spinner in text view in same activity but I don't know how to display in activity2 with text view...
Kindly pls help...
Try this it will work:
activity1.class:
Intent i = new Intent(activity1.this,activity2.class);
Bundle b = new Bundle();
b.putString("name", sp.getSelectedItem().toString());
i.putExtras(b);
startActivity(i);
activity2.class:
Bundle b = this.getIntent().getExtras();
String name = b.getString("name");
((TextView)findViewById(R.id.textView1)).setText(name);
You'll need to pass the value you want to display through your intent to launch activity2. Look at the answer posted here Android: How to pass the data to sub-activities? on how to do it. It shows you the concept of how to pass a value from one activity to another.

Categories

Resources