I'm sending a string from one Activity to another Activity with putExtras and getExtras.
Then I displaying this string to a TextView. When I go back and choose another string it ovewrites the previous string in TextView.
I want to place the new string in a new line in textView.
How to do this?
(I'm sending strings with a button)
This is my code in the recieve Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_completed_tasks);
TextView completedTasksView = (TextView)findViewById(R.id.completed_tasks);
Intent intent = getIntent();
String completedTasks = intent.getExtras().getString("completedTasks");
completedTasksView.setText(completedTasks);
}
}
According to: TextView
append(CharSequence text)
Convenience method: Append the specified
text to the TextView's display buffer, upgrading it to
BufferType.EDITABLE if it was not already editable.
So use:
completedTasksView.append("\n" + completedTasks);
You have to store the string that you sent previously using putExtras because next time activity will be recreated again and your previous data will be lost. To save previous String you can create a static arraylist and on getIntent add your string to arraylist and when you want to print use a for loop and create a single string with appended "/n" and set that string to textview like this
For eg.
public static ArrayList<String> list;
To add string to list
list.add(completedTasks);
To create a single string from arraylist
String text="";
for(int i=0;i<list.size();i++)
{
if(text!="")
{
text=text+"\n"+list(i);
}else{
text=list(i);
}
}
textview.setText(text);
NOTE:
Arraylist should be static for app and not within activity so define arraylist in different class then activity
you can create One globle class with one String variable which will store your completedTask Text every time set text when you get new string ...
Create one global String and always append to it:
String completedTasks = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_completed_tasks);
TextView completedTasksView = (TextView)findViewById(R.id.completed_tasks);
Intent intent = getIntent();
completedTasks += "\n" + intent.getExtras().getString("completedTasks");
completedTasksView.setText(completedTasks);
}
}
Related
I am trying to make a button send data from a number input from one activity to another and display that number. When I click the add button it does not change the value and it displays as null.
//EXTRA_NUMBER is initalized under class header as
//public static final String EXTRA_NUMBER = "com.example.operationhydration.EXTRA_NUMBER";
public void addWater()
{
EditText editText = (EditText) findViewById(R.id.water_input);
double number = Double.parseDouble(editText.getText().toString());
Intent intent = new Intent(this, Progress.class);
intent.putExtra(EXTRA_NUMBER,number);
}
//Code in class I am trying to send it to
Intent intent = getIntent();
String number = intent.getStringExtra(AddWater.EXTRA_NUMBER);
TextView textGoal = (TextView) findViewById(R.id.goal_text);
textGoal.setText("" + number);
EDIT:
I got the variable to display but how do I make it stay if I change pages and come back.
I am sending data from one activity to another through intent. I am sending two different strings but getting same value for both variable on next activity.
Here is my code :
public class Quizzes extends ActionBarActivity {
protected static final String QUIZ_TITLE = null;
protected static final String COURSE = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quizzes);
listView = (ListView) findViewById(R.id.listview);
String[] values = new String[] { "Quiz # 2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
Intent intent = new Intent(getApplicationContext(), QuizDetail.class);
intent.putExtra(QUIZ_TITLE, item);
final String course = (String)textview.getText();
intent.putExtra(COURSE, course);
startActivity(intent);
}
});
}
}
If you see i am passing two string intent object :
1. QUIZ_TITLE
2. COURSE
When i debugged the application, I can see values like
1. QUIZ_TITLE = "Quiz # 1"
2. COURSE = "Intro to Computing"
All fine until here, but when i am retrieving these string on other activity, I am getting value "Intro to Computing" for both, here is code from that class.
public class QuizDetail extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_detail);
Intent intent = getIntent();
String quizTitle = intent.getStringExtra(Quizzes.QUIZ_TITLE);
TextView quizTitleTextView = (TextView) findViewById(R.id.quizTitle);
quizTitleTextView.setText(quizTitle+" : TESTING..");
String courseTitle = intent.getStringExtra(Quizzes.COURSE);
TextView courseTitleTextView = (TextView) findViewById(R.id.courseTitle);
courseTitleTextView.setText(courseTitle);
}
}
I am not sure why I am getting same value "Intro to computing" from Quizzes.QUIZ_TITLE and Quizzes.COURSE.
Any help would be highly appreciated.
Thanks..
Anjum
You are using bad the intent.putExtra(),
You need to put a key (you need to know) as first param, to get the object in the other activity like:
...
String item = ...;
intent.putExtra("COURSE", item);
...
And you get the extras with:
...
intent.getStringExtra("COURSE");
...
Edited !!!
There's a couple of things here that should be mentioned.
QUIZ_TITLE and COURSE are both null (I can't see where they're set)
When you add something to the Extras Bundle, you're placing values in to a dictionary. The key for this dictionary you're using, in this case, is null. This means the second time you're putting in to the dictionary, QUIZ_TITLE (null) is being replaced with the key COURSE (null).
If you change QUIZ_TITLE and COURSE to an actual String value, it should sort that problem.
The second thing to note, is that there's a difference between getExtraString and getExtras.getString. I have written about this here
Hope that helps.
Please try this
Intent intent = getIntent();
String quizTitle = intent.getExtras().getString(Quizzes.QUIZ_TITLE);
String courseTitle = iintent.getExtras().getString(Quizzes.COURSE);
Update:
Oh now i see it too:
protected static final String QUIZ_TITLE = null;
protected static final String COURSE = null;
is really fatal because using a null value for a key is not useful and even if it is possible you are setting your value for the key 'null' first and overwrite it then by setting the value for key 'null' again.
Change it to:
protected static final String QUIZ_TITLE = "extra_quiz_title"
protected static final String COURSE = "extra_course";
for example
I'm learning Android and I didn't find an answer on the web. I want to save a string from an EditText on my MainActivity to restore it on my third Activity -> Activity3
I want this string to be displayed on a TextView of this Activity.
public class MainActivity extends Activity {
private EditText nomPrenom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nomPrenom = (EditText) findViewById(R.id.nomPrenom);
...................}
in my activity_main.xml :
android:id="#+id/nomPrenom"
android:text="#string/nomPrenom"
and
public class Activity3 extends ListActivity {
private TextView yourName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity3);
yourName = (TextView) findViewById(R.id.nomPrenom);
yourName.setText("take the name of activity 1 here"); //<-- this line causes crash !!
..................}
here is activity3.xml
android:id="#+id/yourName"
android:text="#string/nomPrenom"
How can I do that ?
First of all your question is not so clear.. But as per given i think you need to fetch a data from edittext ( in main activity ) and then pass it to the third activity .
For This you need to use Intent to pass the data from one activity to other activity.
Example -
nomPrenom = (EditText) findViewById(R.id.nomPrenom);
nomPrenom .getText.toString();
Intent in = new Intent(MainActivity.this, Activity3 .class);
in.putExtra("value",nomPrenom .getText.toString()) ;
In Activity3 receive intent like this :-
String strValue = getIntent().getExtras().getString("value");
yourName = (TextView) findViewById(R.id.yourName);
yourName.setText(strValue);
Use Intent.putExtra("Your text here") and pass it to other activities.
or you can take the string from EditText and make the string Global, public and static and then use it in any other class.
You can do it with many ways ...
You can use shared preference so that you can access it from anywhere. But shared preference will retain your value after your application closed.
Another way pass your value with your activity intent when you are creating your activity like this: intent.putExtra' and get value into your activity viagetExtra`
Another way you can define your variable at global place consider create one class too hold such common variables and from there you can access those variable from anywhere.
All three ways can be changed according to your requirement.
In order to move from MainActivity to Activity3 write the following code
//get your edittext's text here
String text = nomPrenom.getText().toString();
//move to Activity3
Intent intent = new Intent(MainActivity.this, Activity3.class);
//pass the edit text value to Activity3
intent.putExtra("EditTextValue", text);
//start Activity3
startIntent(intent);
In Activity3, to get your edit text value do the following.
//get your edit text value here which is passed in MainActivity
String text = getIntent().getStringExtra("EditTextValue");
The variable "text" is the final output which you are looking for.
My question is a bit basic. I've been learning to code on JAVA and android. Here I am a bit confused on how to call the values that I have sent via an intent.
In my first activity this is the intent that I am using.
Intent intent = new Intent(MainActivity.this, Secondactivity.class);
String regName1 = regName;
intent.putExtra(regName1, regNameSplit[0]);
startActivity(intent);
Here regName1 will contain three values. SessionID,URL,Name split by "-".
In my SecondActivity
public class Secondactivity extends Activity {
public final String TAG = "###---Secondactivity---###";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity);
Log.i(TAG,"before if statement");
if (getIntent().getStringExtra("regName1") != null){
getIntent().getStringExtra("regName1").split("-");
String[] str = "regName";
Log.i(TAG, ""+str[0]+str[1]+str[2])
}
}
}
The value if regName1 always comes as null.
This line
intent.putExtra(regName1, regNameSplit[0]);
Needs to be like this instead
intent.putExtra("regName1", regNameSplit[0]); // note the quotes
BUT you are using regName1 as a variable... how do you expect the second class to know that variable?
Use a string resource instead.
and you are sure that the content of the variable regName is actually "regName"?
cause you set the value using
intent.putExtra(regName, ... )
and you get the value using
intent.getStringExtra("regName")
use firstactivity
dont use
String regname1=regname;
just:
intent.putExtra("regName1", regNameSplit[0]);
in second Activity
if (getIntent().getStringExtra("regName1") != null){
//
}
I am trying to use putExtra() and getExtra() to send String Data from one activity to another, such that the retrieved string is to be displayed on a TextView and when running.
When i run the program i get a classCastException on onCreate() method.
I am new to android so any assistance will be appreciated.
Here is my sample code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sales);
//TextView
TextView model1 = (TextView)findViewById(R.id.model1);
TextView model2 = (TextView)findViewById(R.id.model2);
TextView model3 = (TextView)findViewById(R.id.model3);
//Bundle
Bundle bundle = getIntent().getExtras();
String Mod1 = bundle.getString("model1");
String Mod2 = bundle.getString("model2");
String Mod3 = bundle.getString("model3");
//setting values
model1.setText(Mod1);
model2.setText(Mod2);
model3.setText(Mod3);
}
Your widgets model1,model2,model3 are either not TextView or you do not pass strings to the intent that you pass to the new Activity. Also you could try to clean your projects, maybe your R.java file is messed up. You could also paste the LogCat or tell us which is the line that gives you the ClassCastException.