gosulove gosulove - 4 months ago 24
Android Question

Android Studio onClick button to display ImageView in another activity (part 2)

I think i didn't describe properly what i meant in this question part 1 . So i enter more details here . Please have a look .

activityOne.java

EditText get_input;
Button get_button;
String display;
int image_id;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);

get_input=(EditText)findViewById(R.id.input_year);
get_button=(Button)findViewById(R.id.button_submit);

get_button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {


int store_get_input=Integer.parseInt(get_input.getText().toString());
int results;
results=store_get_input%12;


if (results == 0) {

display = "Your number is "store_get_input";
image_id=0;

} else if (result ==1){

display = "Your number is "store_get_input";
image_id=1;

}else{

display = "Your number is others";
image_id=3;
}

Intent home_intent=new Intent(getApplicationContext(),Result_page.class);
home_intent.putExtra("home_store_get_input",display);
home_intent.putExtra("home_store_image_src",image_id);
startActivity(home_intent);

}
});
}


activityTwo.java

public class Result_page extends AppCompatActivity {

TextView get_result;
ImageView get_image;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result_page);


get_result=(TextView)findViewById(R.id.display_result);
get_image=(ImageView)findViewById(R.id.image_view);

Intent result_intent=getIntent();
String text=result_intent.getStringExtra("home_store_get_input");

get_result.setText(text);// display what the user has enter

// get_image.setImageResource
//OR other coding to display imageview by using the image_id from activty 1?
}


From both java files, i think it will be clearer and easier for u guys to read. So i stuck at activityTwo.java the last past where I need to key in some codes in order to display the imageview by uising int image_id or other coding.

Answer

I don't think you need 2 duplicated questions.

Method 1: http://stackoverflow.com/a/38342798/2670771

Method 2: In activity 1 just send image id to activity 2

if (results == 0) { 
   display = "Your number is "store_get_input";
   image_id = R.drawable.image0; 
} else if {...}

In activity 2 get image id from intent

int defaultIdWhenCouldNotFindImageSourceId = -1;//should use a negative value 
int imageId = result_intent.getIntExtra("home_store_image_src", defaultIdWhenCouldNotFindImageSourceId);
if( defaultIdWhenCouldNotFindImageSourceId != imageId) {
    //when you can find an valid image id in intent extra, display it
    get_image.setImageResource(imageId);
}
Comments