0

I have three different classes and when the ImageButton in class1 is clicked I want that the TextView in class3 should change to "50". On the other hand when the ImageButton in class2 is clicked I want that the TextView in class3 should change to "0".

class1:

ImageButton button1 = (ImageButton) this.findViewById(R.id.imageButton);
    if (button1 != null) {
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent passdata_intent1 = new Intent(class1.this, class3.class);

                String data1 = "50";

                Bundle bundle1 = new Bundle();

                bundle1.putString("firstdata", data1);

                passdata_intent1.putExtras(bundle1);


                startActivity(passdata_intent1);

            }
        });
    }

class2:

ImageButton button1 = (ImageButton) this.findViewById(R.id.imageButton);
    if (button1 != null) {
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

              Intent passdata_intent2 = new Intent(class2.this, class3.class);

                String data2 = "0";

                Bundle bundle2 = new Bundle();

                bundle2.putString("seconddata", data2);

                passdata_intent2.putExtras(bundle2);

                startActivity(passdata_intent2);



            }
        });
    }

class3:

TextView score = (TextView) findViewById(R.id.textViewscore);


        Bundle bundle1 = getIntent().getExtras();

        String data_1 = bundle1.getString("firstdata");

        score.setText(data_1);




        Bundle bundle2 = getIntent().getExtras();

        String data_2 = bundle2.getString("seconddata");

        score.setText(data_2);

So my problem is that when I start the application and I click on the ImageButton in class2 the TextView in class3 changes. But when I click the ImageButton in class1 nothing changes in class3.

  • 2
    Bcoz you set `score.setText();` two times . So the last method call every time and it not showing first value data inside your textview – sushildlh Sep 24 '16 at 12:25

2 Answers2

0

Yo uare overriding the score value in both cases. If else logic will work fine.

    if(getIntent().hasExtras("firsdata")){

        Bundle bundle1 = getIntent().getExtras();

        String data_1 = bundle1.getString("firstdata");

        score.setText(data_1);

    } else{

        Bundle bundle2 = getIntent().getExtras();

        String data_2 = bundle2.getString("seconddata");

        score.setText(data_2);
    }
ugur
  • 3,604
  • 3
  • 26
  • 57
0

From the snippets of code I see the problem seems to be that you first check for "firstdata" extra in the intent set it to the text view and then you check for "seconddata" extra and override the value in the text view with it.

When you pass the firstdata to the activity the seconddata (if not passed) should be null and thus you set the score text to null and erase the first data value from it.

There is no need to user 2 different names for the extras in order to pass data to the same textview from 2 different entry points. Use "firstdata" extra name for both class1 and class2 to pass the data and it should work.

eXirrah
  • 41
  • 3