1

I'm trying to set TextView visibility in one activity based on an integer value from another activity. Ideally, I am trying to set the value of imgID as an int, and depending on the integer value, the second activity sets the visibility of 10 TextView's. imgID value is supposed to be set to a number whenever a ImageView is clicked in the main activity (Pictures). However when I run the program, all the text appears regardless of which picture I click on. This is my second lab in a class that I'm struggling a bit with, so my apologies if I've missed something obvious. Also, any other tips or info on other things I've missed are appreciated. Thanks for the help ahead of time!

Code is as follows:

Main Activity:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class Pictures extends AppCompatActivity {
    static int imgID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pictures);

        //register click event listener to image view(I hope :)
        ImageView imgB = (ImageView) findViewById(R.id.imageViewB);
        ImageView imgH = (ImageView) findViewById(R.id.imageViewH);
        ImageView imgM = (ImageView) findViewById(R.id.imageViewM);
        ImageView imgD = (ImageView) findViewById(R.id.imageViewD);
        ImageView imgS = (ImageView) findViewById(R.id.imageViewS);
        imgB.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //if imgB clicked, set imgID to 1 and so on...
                 imgID = 1;
                startActivity(new Intent(Pictures.this, Lab2App2.class));
            }
        });


        imgD.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View v) {
                imgID = 2;
                startActivity(new Intent(Pictures.this, Lab2App2.class));
            }
        });



        imgH.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View v) {
                imgID = 3;
                startActivity(new Intent(Pictures.this, Lab2App2.class));
            }
        });


        imgM.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View v) {
                imgID = 4;
                startActivity(new Intent(Pictures.this, Lab2App2.class));
            }
        });
        imgS.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View v) {
                imgID = 5;
                startActivity(new Intent(Pictures.this, Lab2App2.class));
            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_pictures, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);

    }




}

Second Activity:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class Lab2App2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lab2_app2);
        TextView campusB = (TextView)findViewById(R.id.text_campusB);
        TextView campusD = (TextView)findViewById(R.id.text_campusD);
        TextView campusH = (TextView)findViewById(R.id.text_campusH);
        TextView campusM = (TextView)findViewById(R.id.text_campusM);
        TextView campusS = (TextView)findViewById(R.id.text_campusH);
        if(Pictures.imgID == 1) {

            campusB.setVisibility(View.VISIBLE);
            campusD.setVisibility(View.GONE);
            campusH.setVisibility(View.GONE);
            campusM.setVisibility(View.GONE);
            campusS.setVisibility(View.GONE);

        }
        else if(Pictures.imgID == 2) {

            campusB.setVisibility(View.GONE);
            campusD.setVisibility(View.VISIBLE);
            campusH.setVisibility(View.GONE);
            campusM.setVisibility(View.GONE);
            campusS.setVisibility(View.GONE);

        }
        else if(Pictures.imgID == 3) {

            campusB.setVisibility(View.GONE);
            campusD.setVisibility(View.GONE);
            campusH.setVisibility(View.VISIBLE);
            campusM.setVisibility(View.GONE);
            campusS.setVisibility(View.GONE);

        }
        else if(Pictures.imgID == 4) {

            campusB.setVisibility(View.GONE);
            campusD.setVisibility(View.GONE);
            campusH.setVisibility(View.GONE);
            campusM.setVisibility(View.VISIBLE);
            campusS.setVisibility(View.GONE);

        }
        else if(Pictures.imgID == 5) {

            campusB.setVisibility(View.GONE);
            campusD.setVisibility(View.GONE);
            campusH.setVisibility(View.GONE);
            campusM.setVisibility(View.GONE);
            campusS.setVisibility(View.VISIBLE);

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_lab2_app2, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
Matt
  • 81
  • 2
  • 11

2 Answers2

1

In android normally you should pass parameters like your imgID to an intent and then get those values from the new startet activity.

Here a short code example:

  Intent intent = new Intent(Pictures.this, Lab2App2.class);
       intent.putExtra("imgID", imgID);

        //startActivityForResult(intent, YOUR_RETURN_CODE);
        startActivity(intent); 

The RETURN_CODE is needed in case you call the acitivtiy with startActivitiyForResult() so you know which activity returned your result.

In the Lab2App2 Activity you can grab the int by using:

getIntent().getIntExtra("imgID",DEFAULT_VALUE);

It's not a good practice to access directly static variables. There are tons of different ways how you can do it. But that is the safest and easiest solution... For example for restoring instance state ect. it is always recommended to use the intent.

I hope this helps a little bit. Cheers

Patric
  • 342
  • 4
  • 19
0

You better use Intent, you can create a Intent with putExtras as follows :

Intent intent = new Intent(Pictures.this, Lab2App2.class);
intent.putExtra("imgID", imgID);
startActivity(intent);

Then in your onCreate() in your Lab2App2 you add this :

int _imgId = getIntent().getIntExtra("imgID"); 

Now you can compare as you were doing

if(_imgId == 1) {
 ....
}

Remember to remove the static from your first Activity (static int imgID;)

By the way... I don't know if it was a miss copy & paste or you want this way but you have this in your code :

TextView campusS = (TextView)findViewById(R.id.text_campusH); 

It should be campusS, shouldn't be? :D

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • Hmmm... @Skizo that looked like it was going to work, although I did have to add a default value of '0' to `int _imgId = getIntent().getIntExtra("imgID");` However, I'm still getting the same problem as before, with all the text appearing. Any other suggestions? – Matt Sep 16 '15 at 03:07
  • Can you ensure that the int passed grom first activity to second one is well passed, i mean make a toast or something and let me know – Skizo-ozᴉʞS ツ Sep 16 '15 at 08:56
  • Hey, thanks! I got a classmate to help me figure out the last bit, but you set me on the right track. Turned out I forgot to remove the `startActivity(new Intent(Pictures.this, Lab2App2.class));` after applying the code you gave me. Also you were correct about the campusS, I labled the second part incorrectly. Really appreciate the help! – Matt Sep 16 '15 at 21:36
  • Awesome! If my answer helped to you feel free to mark it as a correct and if you have more issues with your code dont doubt to ask it to me ill help you :) – Skizo-ozᴉʞS ツ Sep 16 '15 at 23:17