0

The scenario I have now is, I am going to start 2 activity to get some String value, then on the 3rd activity, I will capture the String value and then I will compare the captured String and change the image source

example:

  1. choose programme
  2. choose day (monday~friday)
  3. captured the String pass from programme to day, and combine both and pass to activity with ImageView

once again i have edited the code.

1st activity

enter code here

public class TestSchedule extends Activity {

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

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

public void SSD(View view){
    String programme = "SSD";
    Intent intent = new Intent(TestSchedule.this,TextDay.class);
    intent.putExtra("prog", programme);
    startActivity(intent);
    }

public void EIS(View view){
    String programme = "EIS";
    Intent intent = new Intent(TestSchedule.this,TextDay.class);
    intent.putExtra("prog", programme);
    startActivity(intent);
    }

public void IS(View view){
    String programme = "IS";
    Intent intent = new Intent(TestSchedule.this,TextDay.class);
    intent.putExtra("prog", programme);
    startActivity(intent);
    }

public void IT(View view){
    String programme = "IT";
    Intent intent = new Intent(TestSchedule.this,TextDay.class);
    intent.putExtra("prog", programme);
    startActivity(intent);
    }

}

2nd activity

public class TextDay extends Activity {

String programme;

private void getData(){
    Bundle extras = getIntent().getExtras();
    if(extras != null){
        programme = extras.getString("prog");
    }
}

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

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

public void monday(View view){
    if(programme.equals("SSD")){
        String programDay = "SSDMon";
        Intent intent = new Intent(TextDay.this, TestShow.class);
        intent.putExtra("progDay", programDay);
        startActivity(intent);
    }
    if(programme.equals("EIS")){
        String programDay = "EISMon";
        Intent intent = new Intent(TextDay.this, TestShow.class);
        intent.putExtra("progDay", programDay);
        startActivity(intent);
    }
    if(programme.equals("IS")){
        String programDay = "ISMon";
        Intent intent = new Intent(TextDay.this, TestShow.class);
        intent.putExtra("progDay", programDay);
        startActivity(intent);
    }
    if(programme.equals("IT")){
        String programDay = "ITMon";
        Intent intent = new Intent(TextDay.this, TestShow.class);
        intent.putExtra("progDay", programDay);
        startActivity(intent);
    }   
    }
}

3rd activity which will capture and change the image source

enter code here

public class TestShow extends Activity {

ImageView image;
String table;

private void getData(){
    Bundle extras = getIntent().getExtras();
    if(extras != null){
        table = extras.getString("progDay");
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test_show);
    getData();
    image = (ImageView) findViewById(R.id.showTimeTable);
    if(table.equals("SSDMonday")){
        image.setImageResource(R.drawable.ssd_mon);
    }
    if(table.equals("EISMonday")){
        image.setImageResource(R.drawable.eis_mon);
    }
    if(table.equals("ISMonday")){
        image.setImageResource(R.drawable.is_mon);
    }
    if(table.equals("ITMonday")){
        image.setImageResource(R.drawable.it_mon);
    }
}

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

}
Waymon
  • 3
  • 4

4 Answers4

0

Use bundle when starting new activity

Bundle b = new Bundle;

b.putString(s,key); intent.putExtra(b); startActivity(intent);

More: http://www.vogella.com/articles/AndroidIntent/article.html

Unii
  • 1,587
  • 15
  • 33
0

Like this

Sending activity ActivityA

Intent newActivity1 = new Intent(ActivityA.this, ActivityB.class);
newActivity1.putExtra("choose_programme", position);
startActivity(newActivity1);

Receiving Activity ActivityB

String value = getIntent().getExtras().getString("choose_programme");
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
  • actually i have no idea with the argument that i should put. like putExtra(?, ?) what is the 1st and 2nd parameter for? – Waymon Dec 05 '13 at 07:16
  • First parameter is KEY, second parameter values you want to pass. ex : i want to pass MONDAY (string) to activityB : newActivity1.putExtra("days", "MONDAY"); you can get that value in activityB like this : String value = getIntent().getExtras().getString("days"); fell free to ask if are not clear still – RajaReddy PolamReddy Dec 05 '13 at 07:23
  • i just figured it out while looking at 1 of the answer below. anyway thanks, still trying – Waymon Dec 05 '13 at 07:33
  • thx for the answer, i have finally able to do it. – Waymon Dec 05 '13 at 09:10
0

Usually the solution for this is to put the String as a extra to the intent and together with this extra string send the intent to the other activity

Check this link for more details on how to implement this : Pass a String from one Activity to another Activity in Android

ANSWER IN THE COMMENTS SECTION.

Community
  • 1
  • 1
cokeby190
  • 609
  • 7
  • 14
  • i have take a look at the code, where should i get the intent? during onCreate? – Waymon Dec 05 '13 at 07:34
  • I think Jgdsh Seerm's answer is really clear and should be the explanation for your question. (: – cokeby190 Dec 05 '13 at 07:52
  • i have just updated the code i am using now. could you take a look? becuase i dint know how the send picture work. coz i am going change the image using the string i get from activity 1 and 2. then combine them 2 and compare at the last activity and change the image in my drawable with the same string – Waymon Dec 05 '13 at 07:55
  • Instead of calling Intent i = getIntent() at the top as a variable, you should create a new method maybe called private void getData() { Bundle extras = getIntent().getExtras(); if (extras != null) { programme = extras.getString("prog"); if(programme.equals("SSD")){ .... .... Then you call the method getData in your OnCreate method of your TestDay Class. – cokeby190 Dec 05 '13 at 07:59
  • i have tired what you teach me, it dint give me errors, but the image dint change. – Waymon Dec 05 '13 at 08:30
  • check my code. i have edit it base on what you taught me. – Waymon Dec 05 '13 at 08:35
  • Hi whats the error you are seeing now? Is it still nullpointer at the 2nd activity? – cokeby190 Dec 05 '13 at 08:41
  • 2nd acitivity dint give me nullpointer but it dint change the image resource as i have changed it in 3rd activity with the if else. now i dont know whether is the string put successful or not. or is the reading got problem. – Waymon Dec 05 '13 at 08:45
  • the things you taught me is workable, just my silly mistake, i set wrong progday. anyway thanks – Waymon Dec 05 '13 at 09:11
0

1 Send the Image to the next Activity...You can send the Bitmap because it implements Parcelable

private void sendImage() {
  //Getting the Bitmap from the imageView
  BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable(); 
  Bitmap bitmap = drawable.getBitmap();

  Intent intent = new Intent(MainActivity.this, NextActivity.class);
  intent.putExtra("PROGRAMME", "PROGRAMME_VALUE");
  intent.putExtra("DAY", "DAY_VALUE");
  intent.putExtra("IMAGE", bitmap);
  startActivity(intent);
}

2 Get the image in the NextActivity

Bundle extras = getIntent().getExtras();
if (extras != null) {
   // Bitmap
   Bitmap image = (Bitmap) extras.get("IMAGE");
   if (image != null) {
     imageView.setImageBitmap(image);
   }
   // String value
   String programmeValue = intent.getString("PROGRAMME");
   String dayValue = intent.getString("DAY");
}
Jagadesh Seeram
  • 2,630
  • 1
  • 16
  • 29
  • actually i have implemented the image in my drawable, i just want to get programme as in string and day in string, so i could know which day and which programme. and i will use if else to open for different png in my imageview. and i still need the bitmap? i couldnt understand how the bundle and bitmap do. – Waymon Dec 05 '13 at 07:44
  • since i am having 3 activity. can i do it this way? i declare 2 intent, which is ii and intent. then i do ii.putExtra() this will put extra straight to the 3rd activity, then i startActivity(intent) which only start the activity2. so i can put another string from activity2 to activity3. and finally i capture all 2 intent? – Waymon Dec 05 '13 at 08:02