Yusril Fahrizal
Yusril Fahrizal

Reputation: 21

how to get string from one intent to another intent in android

i tried to get String from one intent to another intent. i already tried to get that but always error. here my code :

i = new Intent(this, JadwalKeberangkatan.class);

btnCari.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    String getAsal = txtAsal.getText().toString();
            String getTujuan = txtTujuan.getText().toString();

            String words[] = getAsal.split(" ");
            String last = words[words.length - 1]; // parsing asal
            String s = last.replaceAll(Pattern.quote("("), "");
            String s1 = s.replaceAll(Pattern.quote(")"), "");
            String asal = s1;

            String word[] = getTujuan.split(" ");
            String lastw = word[word.length - 1]; // parsing tujuan
            String t = lastw.replaceAll(Pattern.quote("("), "");
            String t1 = t.replaceAll(Pattern.quote(")"), "");
            String tujuan = t1;

             //get tanggal
            int day = dtKeb.getDayOfMonth();
            int month = dtKeb.getMonth() + 1;
            int year = dtKeb.getYear();
            String hari = Integer.toString(day);
            String bulan = Integer.toString(month);
            String tahun = Integer.toString(year);
            String tanggal = ""+hari+"-"+bulan+"-"+tahun;

            Bundle bundle = new Bundle();
            bundle.putString("asal", asal);
            bundle.putString("tujuan", tujuan);
            bundle.putString("tanggal", tanggal);

            startActivity(i);
        }
 }

and here code for JadwalKeberangkatan class

public class JadwalKeberangkatan extends Activity {

Intent intent = getIntent();
String asal = intent.getExtras().getString("asal");
String tujuan = intent.getExtras().getString("tujuan");
String tanggal = intent.getExtras().getString("tanggal");

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.jadwal);       
    TextView tx1 = (TextView)findViewById(R.id.textView1);
    tx1.setText(asal);
    TextView tx2 = (TextView)findViewById(R.id.textView2);
    tx2.setText(tujuan);
    TextView tx3 = (TextView)findViewById(R.id.textView3);
    tx3.setText(tanggal);
   }    
}

if i remove intent intent = getIntent(); then tx1 set to "hello", this program is running properly. so what can i do to get string from another intent?

Upvotes: 2

Views: 781

Answers (1)

toadzky
toadzky

Reputation: 3846

You are putting the 3 strings into a Bundle, not the intent passed to the activity. Instead of:

Bundle bundle = new Bundle();
bundle.putString("asal", asal);
bundle.putString("tujuan", tujuan);
bundle.putString("tanggal", tanggal);

startActivity(i);

do:

i.putExtra("asal", asal);
i.putExtra("tujuan", tujuan);
i.putExtra("tanggal", tanggal);

Then the strings will actually get to the next activity.

Upvotes: 2

Related Questions