Displaying text from another activity on a TextView - android

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.

Related

Error: unknown class: 'message' - how to fix that?

I just started teaching myself android development and I am running into an error on this basic hello world application.
I tried searching but everything seems like chinese language to me.
here is the tutorial i was following: https://developer.android.com/training/basics/firstapp/starting-activity
and here is a screenshot of my error:
[I will include the code below][1]: https://i.stack.imgur.com/Xm1tg.png
<code>
public class DisplayMessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
}
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView);
textView.setText(message);//on this line i get an error under "message"
</code>
Move that code in onCreate() method like :
public class DisplayMessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView);
textView.setText(message);
}
}
You need to move the following code inside onCreate()
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView = findViewById(R.id.textView);
textView.setText(message);//on this line i get an error under "message"

Pass a new string in TextView in new line

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);
}
}

Need help to save/restore a string between activities

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.

Working with new instance of a class in Android

Totally new and starting out just working through the app tutorials. I wanted to run an algorithm to test out how it runs. I have it working all perfectly in java but am not sure how to create a class instance correctly in android.
public class DisplayMessageActivity extends Activity {
myAlgorithm myAlg = new myAlgorithm();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
However as soon as run the program I get a force close. I take it this isn't the correct way to do it with Android?
TIA

How can i pass a string value, that has been created in an if statement, through a bundle inside another if statement?

public class MainActivity extends Activity implements OnClickListener, AdapterView.OnItemSelectedListener, OnCheckedChangeListener{
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
//Declare Variables to be used
final Bundle bundle = new Bundle();
final CheckBox checkbox01 = (CheckBox) findViewById(R.id.CheckBox01);
//All if statements for builds
final Button findbuilds = (Button) findViewById(R.id.btn_FindBuildsTab1);{
findbuilds.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if ((v==findbuilds)&&(checkbox01.getText()==("Ahri-AP"))&&((checkbox01).isChecked())){
final String charactername = "Ahri-AP";
}
if ((checkbox01).isChecked(){
//All Bundles are placed here
bundle.putString("CharacterName Value Key", charactername);
//Start Tabhost Activity and pass bundle(s)
Intent nextActivity = new Intent(v.getContext(),
nextActivity.putExtras(bundle);
startActivity(nextActivity); TabHostActivity.class);
}
Basically, what i am trying to do is create the string for the character's name in the first if-statement, and then bundle it in another if-statement. The reason i need to bundle the string(s) in a different if-statement is to avoid reaching the bytes limit of a method. I have a total of 64 different Strings that need to be bundled for each if-statement, and i have a total of 126 if-statements that need to happen. The problem is of course that the String variable is never found or resolved with the bundle in the 2nd if-statement. Is there a way to do this? Or maybe a more efficient way? Thanks in advance for the help! :)
Just pull the definition of your variable outside of the if statement. eg:
final String charactername
if() {
charactername = ...
}
if() {
bundle stuff with charactername
}

Categories

Resources