0

(Edited to be able to compare string) I have 1 TextView, and 2 buttons, one button is "Month" the other button "Week". Im trying to change the Textview accordingly to the button pressed e.g Month,Week When i start the activity for the first time it displays the "Month" as expected.

After when i press "Week" button always displays the "Week" in the TextView, even if i click on "Month" button still shows Week view.

debugging says that when i press "Month" , "Month"= "true", then onCreate the value is still "True", but in the 1st If statement

if (extras != null){         // <-------here is still true
    month = extras.getString(month);  // <-------here is false
}

that value suddenly goes to "false"

I know i could settext in the buttons, but later on i will add graphs to display data, so i would like to be done onCreate. Every times it creates the view, will check which view is selected(by comparing the string) and display the message and graphs. first time run to display the Month view.

What am i doing wrong?

Heres the code

package com.isma.report;



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class report1 extends Activity{


    TextView viewtime;
    String month = "true";
    Intent starterIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.report);

        starterIntent = getIntent();


        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            month = extras.getString("month");

        }

        // Set the text
        viewtime = (TextView) findViewById(R.id.StudentReportText);
        if (month.equals("true")){
            viewtime.setText("Monthly View");
        }

        if{month.equals("false")){
            viewtime.setText("Weekly View");
        }


        //Month View Button
        Button bMonth = (Button) findViewById(R.id.monthincome);
        bMonth.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                starterIntent.putExtra(month, "true");
                startActivity(starterIntent); 
                finish();
            }
        });

        //Week View Button
        Button bWeek = (Button) findViewById(R.id.weekincome);
        bWeek.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                starterIntent.putExtra(month, "false");
                startActivity(starterIntent); 
                finish();

            }
        });
    }
}
Blo
  • 11,903
  • 5
  • 45
  • 99
Isma
  • 21
  • 4
  • Can you confirm the id's(monthincome and monthincome) are buttons – Siva Apr 18 '14 at 22:48
  • they are monthincome and weekincome android:id="@+id/monthincome" android:id="@+id/weekincome" – Isma Apr 18 '14 at 22:54
  • some where your accessing object is null debug it and find where you are getting null object. – Siva Apr 18 '14 at 22:59
  • Why are you not using `boolean` instead of `String` for month"? – Karakuri Apr 18 '14 at 23:04
  • @Karakuri i was initially, but to pass the data with .putExtra needs to be (String, Boolean0 – Isma Apr 18 '14 at 23:25
  • You can still do that. Java will automatically convert the primitive `boolean` to a `Boolean` object and back; this is called "auto-boxing" and "auto-unboxing". – Karakuri Apr 18 '14 at 23:27
  • @Karakuri i was initially but the .putExtra needs to be String name in the first argument that is month. if i change month to boolean how do i pass the value to the activity? – Isma Apr 18 '14 at 23:29
  • i changed like i did ininiatlly month boolean but now i have .putExtra underline in red, saying that one of the fixes is changing month to string – Isma Apr 18 '14 at 23:33
  • I am absolutely certain you can add a regular primitive `boolean` as an extra to an `Intent`. I am staring at one such usage in my own code at this very moment. – Karakuri Apr 18 '14 at 23:49
  • You want to change the value in Textview which one is on the same activity means why you are using intent. Just change the value of Text view while clicking the button right. – Siva Apr 19 '14 at 00:55
  • @Siva know that just for writing out i could do that, but because i need to draw some graphs displaying the values for month and week. the textview and the graphs to be displayed after any button been pressed. i need to generate a new view to refresh whats on the screen. – Isma Apr 19 '14 at 13:18
  • even if i had the textview in the buttons, its ok, but i still need to pass a value to the activity, to compare and then display graphs that i will draw after button is pressed. i dont have the graphs code up yet, but the concept is the same, pass a value to compare and then draw. – Isma Apr 19 '14 at 13:25
  • i fixed how to compare the string, but now is always false, i edited the post to explain – Isma Apr 19 '14 at 15:15

2 Answers2

2
if (month == "true")

You can't compare strings in Java using ==. Use equals(...) - example...

if (month.equals("true"))
Squonk
  • 48,735
  • 19
  • 103
  • 135
  • it crashes like that in that line: if (month.equals("true")) – Isma Apr 18 '14 at 22:11
  • why it gets the false value then? if i initialize with true? its passing the false value when press any button right? – Isma Apr 18 '14 at 22:15
  • The point is `if (month == "true")` will never evaluate to a true result and you have an `else` block which will always be executed. If it's crashing with using `equals(...)` then you have another problem in your code - in that case edit your question and post your logcat. – Squonk Apr 18 '14 at 22:22
  • i fixed that, now is always false, i edited the post – Isma Apr 19 '14 at 15:14
0

Finally i managed to fix it!

First thanks to all u helped me.

Now the fix is very simple.

I moved the initialization of the String month to inside of onCreate method with no value. i also extended the if statement to assign the value true if was the firs time running or not pressing any button.

String month = "";

Bundle extras = getIntent().getExtras();
if (extras != null) {
    month = extras.getString(month);

    }
    else{
         month = "true";
    }

I also initialized with no values the month variable in the onClick methods

String month = "";

very simple but it took me 2 days to figure out! ;p thanks again guys

Isma
  • 21
  • 4