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)
Related
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);
I have stored value in First screen and pass that value and set the value in Edit Text of Second screen. While i am back into first screen and change the value and pass again to second screen and set that value in edit text. Here, it shows previous value only doesn't replace that value.
EditText mobilenumber = (EditText) rootView.findViewById(R.id.mobilenumber);
mobilenumber.setText(mobile);
Above mobile is a variable and I stored the value from first screen and pass it to second screen to set that value in mobilenumber edit text.
1)if you are using activity(first screen and second screen). you can send value using intent.
intent.putExtra("mobile",mobile);
and access it in second screen using
getIntent().getExtras().getString("mobile","defaultvalue");
OR
2) Make mobile as global and static variable in first activity
and in second activity(screen)
in onResume()
setText(firstactivity.mobile);
As you said both the screen are fragment you can share a global variable in activity(if both fragment part of same activity) or
can communicate via passing value in bundle would be safest and
reliable way
Using the Bundle
From your first activity :
public void onCreate(Bundle b){
super.onCreate(b);
String number = "" + R.id.IDInYourFirstActivity;
b.putString("mobilenumber", number);
}
in your next activity
public void onCreate(Bundle bd){
super.onCreate(bd);
String mobileNo = bd.getString("mobilenumber");
EditText mobilenumber = (EditText) rootView.findViewById(R.id.mobilenumber);
mobilenumber.setText(mobileNo);
}
You can use a static variable in second screen. Then in first screen when you change the value then
Second.mobile=your value.
It will change the value accordingly.
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.
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 am making an android app in which i have an activity X that displays a list and a button. Activity X calls a listview to display that list. Each list item has a number(textview) and a checkbox. I used a setonclicklistener on the checkbox, so whenever the checkbox is checked i am storing the number associated with it in a string. Now i want that whenever i click the button the msg activity should start and the numbers to be sent are the ones that are checked.
I am using the following code to start the msg activity in my X activity.
Intent msgIntent = new Intent(Intent.ACTION_VIEW, Uri
.fromParts("sms", msgnumbers, null));
msgIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(msgIntent);
Now "msgsnumbers" variable is present in my listview. How do I pass it to this activity X??
I found the same question here but with no appropriate solution.
-Thanks in advance
Intent in = new Intent(Quote.this, Purchase Ysn.class);
in.putExtra("price", salesprc);
public static String price = "price";
if (getIntent().getExtras().containsKey(price)) {
purces_nbcpy = getIntent().getExtras().getDouble(price);
}
onItemClickListener for ListView has a param position that tells you what position has been clicked.
so if you are using an ArrayList (for eg) to provide values for listItems in adapter you can use this inside onItemClickListener
MyBeanObject object=arraList.get(position);
//use getters of object to retrieve values and pass it as intent
//where arrayList may be your list of objects MyBeanObject