Buttons isn't setting value - java

Im brand new to android and I have no idea what I'm doing wrong. The current code looks like:
public class TypeActivity extends Activity {
private boolean alcoholin = false;
...
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.search_type);
alcohol = (Button) findViewById(R.id.alcohol_button);
...
alcohol.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alcoholin=true;
Intent i = new Intent (TypeActivity.this,ingredients.class);
startActivity(i);
}
});
...
public boolean getalcholin(){
return alcoholin;
}
This code is then supposed to set a value in another class. I have tested the code and I know that if i state the the boolean is true in the beginning of my code, then I will make the other code's boolean equal true. However, if I try to set the value when the user presses the button the value does not get updated.
Please help!

On Android the standard way to send data from one Activity to another is by specifying "extras" on the Intent that you use to start a new activity.
You are already using an Intent in your onClick method to start your "ingredients" activity (Your code would be more readable if you named your activity something like IngredientsActivity instead) - you just need to add some "extras" to it.
Please read up on the training tutorial here, but without knowing what your ultimate goal is you probably want something like:
alcohol.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent (TypeActivity.this,IngredientsActivity.class);
i.putExtra(IngredientsActivity.EXTRA_INGREDIENT_TYPE, "alcohol");
startActivity(i);
}
});
... and then in IngredientsActivity you would have something like:
public static final String EXTRA_INGREDIENT_TYPE = "ingredient";
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ingredients);
String ingredientType = getIntent().getStringExtra(IngredientsActivity.EXTRA_INGREDIENT_TYPE);
}

This would never work because alcoholin would NOT exist as this is an entirely different Activity. Why don't you instead use the Extras of the intent to pass data between the two Activities.
Standards
Class names should always have each word capitalized like MyClassObject in java.

Related

Taking data from one activity and storing it in another

I am trying to build a very simple App in Android Studio to practice using Intents to send data from one activity to another. On my Main Activity I have a "Total" TextView and a button. When I click the button, it takes me to a AddNumber Activity where I can type in an integer and press a button. This then takes me back to the Main Activity and adds that integer to the total, updating the TextView. I want to be able to do this multiple times while I've got the app open so the total keeps going up.
I have tried using a ViewModel to store the information so it doesn't get reset every time the Main Activity onCreate method is run. However, every time I do this, it works once (e.g. if I add a 7 in my AddNumber Activity, the Main Activity total goes to 7). However, when I try again, the ViewModel seems to get reset so doesn't remember the 7 that I put in initially, so if I put in an 8, the total on the MainActivity just gets set to 8, rather than 15. Am I making a mistake somewhere that is causing the ViewModel to reset? I understood that ViewModels were a way of storing data while the app is open, but I can't seem to make it work.
Thanks so much!
MainActivity.java code:
public class MainActivity extends AppCompatActivity {
TextView totalTextView;
MainActivityViewModel viewModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
totalTextView = (TextView) findViewById(R.id.totalTextView);
viewModel = new ViewModelProvider(this).get(MainActivityViewModel.class);
totalTextView.setText(Integer.toString(viewModel.total));
getSetIncomingIntent();
}
public void addNumber(View view){
Intent intent = new Intent(this, AddNumber.class);
startActivity(intent);
}
public void getSetIncomingIntent() {
Intent incomingIntent = getIntent();
if (incomingIntent.hasExtra("value")) {
viewModel.newValue = incomingIntent.getIntExtra("value",0);
viewModel.addNumber();
totalTextView.setText(Integer.toString(viewModel.total));
}
}
}
MainActivityViewModel.java code:
public class MainActivityViewModel extends ViewModel {
int total = 0;
int newValue;
public void addNumber() {
total = total + newValue;
}
}
AddNumber.java code:
public class AddNumber extends AppCompatActivity {
EditText numberEditText;
int value;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_number);
numberEditText = (EditText) findViewById(R.id.numberEditText);
}
public void addNumber(View view){
value = Integer.parseInt(numberEditText.getText().toString());
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("value", value);
startActivity(intent);
}
}
When I click the button, it takes me to a AddNumber Activity where I can type in an integer and press a button. This then takes me back to the Main Activity and adds that integer to the total, updating the TextView.
This is not what is happening. First you create a new AddNumber activity.
public void addNumber(View view){
Intent intent = new Intent(this, AddNumber.class);
startActivity(intent); // This launches a new AddNumber Activity
}
And then here you are creating a new MainActivity instance.
public void addNumber(View view){
value = Integer.parseInt(numberEditText.getText().toString());
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("value", value);
startActivity(intent); // This launches a new MainActivity
}
So you're basically building a giant stack of MainActivity / AddNumber instances.
You need to start AddNumber with startActivityForResult and then handle that in MainActivity. Then you will be working with the original MainActivity and the original ViewModel and you will be able to update it.
Please refer to the documentation on starting an Activity for result.
Hope that helps!

In Android Studio, cannot invoke Activity B from Activity A more than once

Further to my previous post, I now want to invoke a child activity from the main activity a number of times. In my real project (as opposed to the noddy test below), when the child activity is invoked, its header displays, "Enter first data set" then invites the user to enter some data. This data is actually stored in a common class rather than being returned to the main activity. Then the child needs to be called again with a new prompt "Enter second data set", and the same thing happens.
What I cannot work out is how to do this. If I include two calls to the child, every time, only the second call appears to happen, the prompt appearing in the child activity being "Enter second data set" every time. This startActivityForResult() method is I believe, designed to be used when you want to call an activity and wait for the result (which you do with an onActivityResult() do you not), but it does not wait.
How on earth do I do this? Sample code follows.
Thank you to anyone who can clearly explain where I'm going wrong and what the right code should be.
MainActivity code extract
#Override
public void onResume(){
super.onResume();
TextView maintop = (TextView)findViewById(R.id.maintop);
maintop.setText(Common.mess1);
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mainbutton = (Button)findViewById(R.id.mainbutton);
mainbutton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent intent1 = new Intent(MainActivity.this,Child.class);
intent1.putExtra("Prompt", "Enter first data set");
startActivityForResult(intent1,1);
onActivityResult(1,1,intent1);
}
});
mainbutton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent intent2 = new Intent(MainActivity.this,Child.class);
intent2.putExtra("Prompt", "Enter second data set");
startActivityForResult(intent2,1);
onActivityResult(1,1,intent2);
}
});
}
You can only have one click listener in the button, so when you call set for the 2nd time it replaces the listener.
What you need to do is set the click listener for the enter first data, don't call to onActivityResult(1,1,intent1) that's not how you do it, you need override the method, and in onActivityResult call the 2nd.
Something like this:
static final int FIRST_INTENT = 1;
static final int SECOND_INTENT = 2;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mainbutton = (Button)findViewById(R.id.mainbutton);
mainbutton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent intent1 = new Intent(MainActivity.this,Child.class);
intent1.putExtra("Prompt", "Enter first data set");
startActivityForResult(intent1,FIRST_INTENT);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FIRST_INTENT) {
if (resultCode == RESULT_OK) {
Intent intent2 = new Intent(MainActivity.this,Child.class);
intent2.putExtra("Prompt", "Enter second data set");
startActivityForResult(intent2,SECOND_INTENT);
}
}
}
And in your child activity
//DO SOMETHING
....
setResult(RESULT_OK)
finish();
}
For more check
[http://developer.android.com/intl/es/training/basics/intents/result.html]
[http://developer.android.com/intl/es/reference/android/app/Activity.html#setResult%28int%29]

Passing an integer using Intent not working in Android

I have an Intent set up that opens up a new activity and I wanted to pass an Integer value. The opening of the activity works but as soon as I use a code to pass on the value app crashes.
Here is my code of main activity -
public void onFinish() {
tap1.setClickable(false);
Intent i = new Intent( Single.this, FinalScore.class);
i.putExtra("kee1", count);
startActivity(i);
Value is fetched using below code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final_score);
TextView tx = (TextView) findViewById(R.id.textView3);
tx.setText(getIntent().getExtras().getInt("kee1"));
}
you need cast int value to String for setting to TextView:
you can use following code:
tx.setText(""+getIntent().getExtras().getInt("kee1"));
add "" to first of the value do this for you.
or you can use following code to cast that:
String value = String.valueOf(getIntent().getExtras().getInt("kee1"));
tx.setText(value);
you can use Integer.toString(); too as #Duncan mentioned on comment.

Saving data across rotation does not seem to work in my android project

PLESAE NOTE: The solution to my problem is in bold text at the bottom. I accepted Melquiades's answer because he helped me filter out everything that could have been the problem. It turns out, I was the problem, not android or anything else. So if you are looking for the answer, read below.
I'm trying to save the state of a variable as it is before onPause(); , onStop(); , onDestroy(); are called.
The book I am using has me override a method called
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt(KEY_INDEX, myIntVaraible);
}
the variables you see in the parameter are declared at the beginning of the class
private static final String KEY_INDEX = "index";
private int myIntVariable;
with this method created, the book tells me to then go the the onCreate method and add
if(savedInstanceState != null){
myIntVariable = savedIntanceState.getInt(KEY_INDEX, 0);
}
But this does not work.
Whenever the activity is destroyed and created, the myIntVariable is reset to 0.
What I did to fix this is I went to my manifest file and
added android:configChanges="orientation|screenSize".
However, I have read that this is not practical and is strongly advised against.
EDIT: As suggested, I am adding my onCreate(); and onResume(); methods..
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate()");
setContentView(R.layout.activity_main);
iterateQuestions();
mTrueButton = (Button)findViewById(R.id.trueBt);
mTrueButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
correctPressed = true;
checkForTrue();
}
});
mFalseButton = (Button)findViewById(R.id.falseBt);
mFalseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
falsePressed = false;
checkForFalse();
}
});
mNextButton = (ImageButton)findViewById(R.id.nextBt);
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
try{
mIndexCounter++;
mTextViewQuestion = (TextView)findViewById(R.id.text_question_view);
int QuestionToShow = mQuestionBank[mIndexCounter].getQuestion();
mTextViewQuestion.setText(QuestionToShow);
}
catch(Exception e){
iterateQuestions();
}
}
});
mTextViewQuestion = (TextView)findViewById(R.id.text_question_view);
mTextViewQuestion.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
try{
mIndexCounter++;
mTextViewQuestion = (TextView)findViewById(R.id.text_question_view);
int QuestionToShow = mQuestionBank[mIndexCounter].getQuestion();
mTextViewQuestion.setText(QuestionToShow);
}
catch(Exception e){
iterateQuestions();
}
}
});
mPrevButton = (ImageButton)findViewById(R.id.prevBtn);
mPrevButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
try{
mIndexCounter--;
mTextViewQuestion = (TextView)findViewById(R.id.text_question_view);
int QuestionToShow = mQuestionBank[mIndexCounter].getQuestion();
mTextViewQuestion.setText(QuestionToShow);
}catch(Exception e){
iterateQuestionsReverse();
}
}
});
}
and
#Override
public void onResume(){
super.onResume();
Log.d(TAG,"onResume()");
}
For all intents and purposes, the variable mIndexCounter is the "myIntVariable" I mentioned.
SOLUTION: I was using a book and unfortunately, since I am new to android programming, relied too much on the code written in the book. The authors usually add new code in their book as bold, black text. This time, they failed to do that and I had trouble figuring out why my data was not being saved. It turns out that it was saved all along, I just failed to update the view with the saved data whenever it was retrieved. After adding 3 lines of simple code, my mistake was obvious and the goal I had been trying to accomplish, a success.
My program displayed a string of text that was dependant on an int that was used to retrieve information from the R.java class. After launching the app, when the user presses Next, the data changes because the int is incremented and the String on the view changes.
This data was to be saved due to the nature of android destroying any unsaved data upon orientation change.
All I had to do was simply add the saved data, an int, the same way I used to display this string/text in the first place. Instead, I foolishly assumed it would do it automatically because the book did not add this code and I relied on it too much.
This was a great learning experience and if anyone ever comes across something like this, feel free to email me if my answer is not clear.
Instead of in onCreate(), restore your variable in onRestoreInstanceState():
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
myIntVariable = savedIntanceState.getInt(KEY_INDEX, 0);
}
The docs also say:
Note: Because onSaveInstanceState() is not guaranteed to be called, you should use it only to record the transient state of the activity (the state of the UI)—you should never use it to store persistent data. Instead, you should use onPause() to store persistent data (such as data that should be saved to a database) when the user leaves the activity.
Btw, change your onCreate() signature to:
#Override
protected void onCreate(Bundle savedInstanceState) {
as in the docs.
Try this:
android:configChanges="keyboard|keyboardHidden|orientation"
As stated here

Using onClickListener to take me to another XML file

I quite new/unfamiliar with Java so forgive me if my terminology is a bit off.
To summarize, I have buttons in my XML file which I'd like to take me to another page (like a hyperlink would in HTML, etc.) But when entering this, an error appears with the two setActivity() parts of the code:
The method setActivity() is undefined for the type new View.OnClickListener(){}
Here is the code for the class:
public class HomePage extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
Button button1 = (Button)setActivity().findViewById(R.id.bLogin);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(setActivity(), LinkHome.class);
startActivity(i);
}
});
}
...
Can anyone give me any advice to get around with this?
Try this. It might help to you.
Intent i = new Intent(HomePage.this, LinkHome.class);
startActivity(i);

Categories