Question

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

            }
        });
    }
}
Was it helpful?

Solution 2

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

OTHER TIPS

if (month == "true")

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

if (month.equals("true"))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top