Can I have a text field in an Activity other than your main Activity. I have a list that on click uses an activity which is the same for everyone. However, I want the text fields to change based on which thing you click on the list. For example, if you click 'gym' in the list, I want the name text field to say 'Gym'. How do you do this? Are variables in Main Activity public to other activities?
Thanks!
Intents can be used to send data from one activity to another as shown below
Intent intent = new Intent(getApplicationContext(), Activity2.class);
intent.putExtra("MY_VAL","value");
startActivity(intent);
and it can be retrived at Activity2.class as shown below (write this code in oncreate on your second activity).
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("MY_VAL");
}
Related
What can i do if i want to click on one item(say Avatar movie) out of several items in an expandable view and then a textview appears about description of the(Avatar movie) BUT DESCRIPTION SHOULD APPEAR ON NEXT ACTIVITY and the same repeats for the other items.
Do following steps
Grab the selected value (let it be avatar movie) and send it to next activity by putExtra of Intent. Its like
Intent intent = new Intent(SendingActivity.this,RecievingActivity.class);
intent.putExtra("keyName", "avatar movie");
startActivity(intent);
Get the value from Intent on second activity
Bundle extras = intent.getExtras();
if(extras != null)
String data = extras.getString("keyName");
Get the description related to avatar movie or whatever was selected in second activity.
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");
}
}
So I have a listview in my activity1 class, what i want to do is:
Click on an item in the listview, which will open activity2 class, with 2 edit texts with the values from the clicked item in the listview, like name and age strings, I want to edit those values/strings in my activity2 class [by changing the edit texts], and send the edited values back to my listview in activity1 class, and show the edited values in my listview [for example show the name] instead of showing the old value/string that was in the listview before the edit.
I have tried many different ways, and I couldn't accomplish the goal, I would love if any of you could help me.
Thank you,
You can transfer your selected items data to your second activity like this code below
Intent i = new Intent(MainActivity.this,ReportActivity.class);
i.putExtra("MainDate", MainDate.getText().toString());
and in your second activity you have to get this data's and then manipulate them
Intent intent = getIntent();
MainDate = intent.getExtras().getString("MainDate");
then send back your manipulated data's to your first Activity just like before then update your list adapter
you can have data of your selected item in your first activity list by this code which lies on yourList.setOnItemClickListener
Cursor Getdata = (Cursor)YourList.getItemAtPosition(position);
String Yourcolumnstring = Getdata.getString(Getdata.getColumnIndex("yourcolumnindex")) ;
you should send your strings to activity2 and when retun from activity2 to activity1 should send your new strings from activity2 to activity1.you should your strings send as following:
String str = "My Data"; //Data you want to send
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("name",str);
you should receive str in activity2 as following:
String name = this.getIntent().getStringExtra("name");
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.
If I have a widget that has a textView within it, and then a different class with an activity with a editText, is there a way to change the widget textView value, to the same value of the editText?
I guess you are using an Intent to switch between your activities.
You could use the method putExtra to send some data.
In the activity where you have the textEdit :
Intent intent = new Intent(this, myClass.class);
intent.putExtra("key", "StringFromEditText");
startActivity(intent);
And get it in the activity where you have the textView :
Bundle extras = getIntent().getExtras();
String content = extras.getString("key");
Put the value of the EditText in a a database, file, or SharedPreferences in the activity that it's in, and read that back out in your widget.