2013-07-20 43 views
0

我有兩個活動,我需要將一些字符串和一個字符串構建器從TailorThreeActivity.java傳遞到EnquireActivity.java。從活動之間傳遞字符串和字符串構建器

在EnquireActivity屏幕上顯示字符串和stringbuilder數據後,我打電話通過單擊「發送郵件」按鈕來發送郵件。 「發送郵件」按鈕將打開一個郵件客戶端,並將所有字符串和stringbuilder數據設置爲電子郵件正文。

我能夠在EnquireActivity.java中顯示stringbuilder的值,但來自TailorThreeActivity.java的其他字符串沒有顯示在活動中。儘管在EnquireActivity中顯示了stringbuilder,但在電子郵件消息體中都不顯示字符串和stringbuilder。

這是TailorThreeActivity.java

package com.example.travelplanner; 

import android.os.Bundle; 

import java.util.ArrayList; 
import java.util.Calendar; 
import android.app.Activity; 
import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.view.Window; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.DatePicker; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

public class TailoredthreeActivity extends Activity implements OnClickListener{ 

    TextView txtday1, txtmon1, txtday2, txtmon2; 
    EditText et_name,et_email,et_adult,et_child,et_phone; 
    Button btn; 
    private int year,year1; 
    private int month,month1; 
    private int day,day1; 
    String date1, date2; 
    ArrayList<String> getChecked; 
    static final int DATE_DIALOG_ID = 0; 
    static final int DATE2_DIALOG_ID = 1; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_tailoredthree); 
     et_name = (EditText) findViewById(R.id.editname); 
     et_adult = (EditText) findViewById(R.id.editno_adult); 
     et_child = (EditText) findViewById(R.id.editno_child); 
     et_email = (EditText) findViewById(R.id.editmail); 
     et_phone = (EditText) findViewById(R.id.editphone); 
     txtday1 = (TextView) findViewById(R.id.txtdate_day); 
     txtday2 = (TextView) findViewById(R.id.txtdate_day1); 
     txtmon1 = (TextView) findViewById(R.id.txtdate_mon_year); 
     txtmon2 = (TextView) findViewById(R.id.txtdate_mon_year1); 
     btn = (Button)findViewById(R.id.btn_tailorthree_verify); 
     btn.setOnClickListener(this); 

     Calendar c = Calendar.getInstance(); 
     year = c.get(Calendar.YEAR); 
     month = c.get(Calendar.MONTH); 
     day = c.get(Calendar.DAY_OF_MONTH); 
     year1 = c.get(Calendar.YEAR); 
     month1 = c.get(Calendar.MONTH); 
     day1 = c.get(Calendar.DAY_OF_MONTH); 

     //get Intents 

     Bundle extras = getIntent().getExtras(); 
      if(extras!=null) 
      { 
       getChecked = extras.getStringArrayList("list"); 
      } 

    } 

    @SuppressWarnings("deprecation") 
    public void showdate(View v) { 
     showDialog(DATE_DIALOG_ID); 

    } 

    @SuppressWarnings("deprecation") 
    public void showdate1(View v) { 
     showDialog(DATE2_DIALOG_ID); 
    } 


    public void updateDate() { 
     final String[] MONTHS = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; 
     String mon = MONTHS[month]; 
     txtday1.setText(""+day); 
     txtmon1.setText(mon+"'"+year); 
     } 

    public void updateDate1() { 
     final String[] MONTHS = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; 
     String mon = MONTHS[month]; 
     txtday2.setText(""+day); 
     txtmon2.setText(mon+"'"+year); 
     } 

    private DatePickerDialog.OnDateSetListener datedialog = new DatePickerDialog.OnDateSetListener() { 

     @Override 
     public void onDateSet(DatePicker view, int yy, int mm, 
       int dd) { 
      // TODO Auto-generated method stub 
      month = mm; 
      day = dd; 
      year = yy; 
      date1 = day+"/"+month+"/"+year; 
      updateDate(); 
     } 
    }; 

private DatePickerDialog.OnDateSetListener datedialog2 = new DatePickerDialog.OnDateSetListener() { 

     @Override 
     public void onDateSet(DatePicker view, int yy, int mm, 
       int dd) { 
      // TODO Auto-generated method stub 
      month1 = mm; 
      day1 = dd; 
      year1 = yy; 
      date2 = day1+"/"+month1+"/"+year1; 
      updateDate1(); 
     } 
    }; 

    protected Dialog onCreateDialog(int id) { 
     //new DatePick 
     switch (id) { 
      case DATE_DIALOG_ID: 
       return new DatePickerDialog(this, 
         datedialog, year, month,day); 

      case DATE2_DIALOG_ID: 
       return new DatePickerDialog(this, 
         datedialog2, year1, month1,day1); 
     } 
     return null; 
    } 

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

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     Intent i1 = new Intent(this, EnquireActivity.class); 
     i1.putExtra(et_name.getText().toString(), "name"); 
     i1.putExtra(et_adult.getText().toString(), "adults"); 
     i1.putExtra(et_child.getText().toString(), "child"); 
     i1.putExtra(et_email.getText().toString(), "email"); 
     i1.putExtra(et_phone.getText().toString(), "phone"); 
     i1.putExtra(date1, "datedept"); 
     i1.putExtra(date2, "datearr"); 
     i1.putStringArrayListExtra("list1", getChecked); 
     startActivity(i1); 
    } 

} 

這是EnquireActivity.java

package com.example.travelplanner; 

import java.util.ArrayList; 
import java.util.Calendar; 

import android.os.Bundle; 
import android.app.Activity; 
import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.DatePicker; 
import android.widget.CompoundButton.OnCheckedChangeListener; 
import android.widget.EditText; 

public class EnquireActivity extends Activity implements OnClickListener, OnCheckedChangeListener { 

    //setting var for datepicker 
    private int year,year1; 
    private int month,month1; 
    private int day,day1; 
    String date1, date2; 
    static final int DATE_DIALOG_ID = 0; 
    static final int DATE2_DIALOG_ID = 1; 

    Button btn; 
    StringBuilder stringbuilder, mailbuilder; 
    String to, subject,msg; 
    String getname,getadult, getchild, getmail, getphone, getdatedept, getdatearr; 
    ArrayList<String> getChecked; 
    CheckBox chk; 
    EditText et_citylist, et_name, et_adult, et_child, et_mail, et_phone, et_datedepart, et_datearrive; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_enquire); 
     //registering edittexts 
     et_citylist = (EditText)findViewById(R.id.enquire_edit_city); 
     et_name = (EditText)findViewById(R.id.enquire_edit_name); 
     et_adult = (EditText)findViewById(R.id.enquire_edit_adult); 
     et_child = (EditText)findViewById(R.id.enquire_edit_child); 
     et_mail = (EditText)findViewById(R.id.enquire_edit_email); 
     et_phone = (EditText)findViewById(R.id.enquire_edit_phone); 
     et_datearrive = (EditText)findViewById(R.id.enquire_edit_datearrival); 
     et_datedepart = (EditText)findViewById(R.id.enquire_edit_datedeparture); 

     //setting up email button 
     btn = (Button)findViewById(R.id.btnemail); 
     btn.setVisibility(View.GONE); 
     btn.setOnClickListener(this); 

     //setting up mail content 
     to = "[email protected]"; 
     subject = "Tour Package Details Summary"; 
     msg = "Tour Enquiry Details:\nName: "+et_name.getText().toString()+"\nDestinations: "+et_citylist.getText().toString()+"\nNo. of Adult Members: "+et_adult.getText().toString()+"\nNo. of Child Members: "+et_child.getText().toString()+"\nDeparture: "+et_datedepart.getText().toString()+"\nArrival: "+et_datearrive.getText().toString()+"\nEmail: "+et_mail.getText().toString()+"\nContact No.: "+et_phone.getText().toString(); 

     //setting checkbox 
     chk = (CheckBox)findViewById(R.id.chkenquire); 
     chk.setOnCheckedChangeListener(this); 

     //setting calender components for datepicker 
     Calendar c = Calendar.getInstance(); 
     year = c.get(Calendar.YEAR); 
     month = c.get(Calendar.MONTH); 
     day = c.get(Calendar.DAY_OF_MONTH); 
     year1 = c.get(Calendar.YEAR); 
     month1 = c.get(Calendar.MONTH); 
     day1 = c.get(Calendar.DAY_OF_MONTH); 

     //getting Intents from previous activities 

     Bundle extras = getIntent().getExtras(); 
      if(extras!=null) 
      { 
       getChecked = extras.getStringArrayList("list1"); 
       getname = extras.getString("name"); 
       getadult = extras.getString("adults"); 
       getchild = extras.getString("child"); 
       getmail = extras.getString("email"); 
       getphone = extras.getString("phone"); 
       getdatedept = extras.getString("datedept"); 
       getdatearr = extras.getString("datearr"); 
       stringbuilder = new StringBuilder(); 
       for(String value:getChecked){ 
        stringbuilder.append(value).append(" "); 
       } 
      } 
     //adding citylist to edittext 
      et_citylist.setText(stringbuilder); 
      et_name.setText(getname); 
      et_adult.setText(getadult); 
      et_child.setText(getchild); 
      et_mail.setText(getmail); 
      et_phone.setText(getmail); 
      et_datearrive.setText(getdatearr); 
      et_datedepart.setText(getdatedept); 

    } 

    @SuppressWarnings("deprecation") 
    public void showdate(View v) { 
     showDialog(DATE_DIALOG_ID); 

    } 

    @SuppressWarnings("deprecation") 
    public void showdate1(View v) { 
     showDialog(DATE2_DIALOG_ID); 
    } 

    public void updateDate() { 
     final String[] MONTHS = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; 
     String mon = MONTHS[month]; 
     et_datedepart.setText(date1); 
     } 

    public void updateDate1() { 
     final String[] MONTHS = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; 
     String mon = MONTHS[month]; 
     et_datearrive.setText(date2); 
     } 

    private DatePickerDialog.OnDateSetListener datedialog = new DatePickerDialog.OnDateSetListener() { 

     @Override 
     public void onDateSet(DatePicker view, int yy, int mm, 
       int dd) { 
      // TODO Auto-generated method stub 
      month = mm; 
      day = dd; 
      year = yy; 
      date1 = day+"/"+month+"/"+year; 
      updateDate(); 
     } 
    }; 

private DatePickerDialog.OnDateSetListener datedialog2 = new DatePickerDialog.OnDateSetListener() { 

     @Override 
     public void onDateSet(DatePicker view, int yy, int mm, 
       int dd) { 
      // TODO Auto-generated method stub 
      month1 = mm; 
      day1 = dd; 
      year1 = yy; 
      date2 = day1+"/"+month1+"/"+year1; 
      updateDate1(); 
     } 
    }; 

    protected Dialog onCreateDialog(int id) { 
     //new DatePick 
     switch (id) { 
      case DATE_DIALOG_ID: 
       return new DatePickerDialog(this, 
         datedialog, year, month,day); 

      case DATE2_DIALOG_ID: 
       return new DatePickerDialog(this, 
         datedialog2, year1, month1,day1); 
     } 
     return null; 
    } 



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

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     //Send Mail 
     Intent email = new Intent(Intent.ACTION_SEND); 
      email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to}); 
      //email.putExtra(Intent.EXTRA_CC, new String[]{ to}); 
      //email.putExtra(Intent.EXTRA_BCC, new String[]{to}); 
      email.putExtra(Intent.EXTRA_SUBJECT, subject); 
      email.putExtra(Intent.EXTRA_TEXT, msg); 

      //need this to prompts email client only 
      email.setType("message/rfc822"); 

      startActivity(Intent.createChooser(email, "Choose an Email client :")); 
    } 

    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     // TODO Auto-generated method stub 
     if(chk.isChecked()) 
      btn.setVisibility(View.VISIBLE); 
     else 
      btn.setVisibility(View.GONE); 

    } 

} 
+0

從stringbuilder中獲取文本,並在點擊發送電子郵件按鈕後以字符串的形式傳遞它 – KOTIOS

+0

您是否嘗試使用Log.d()來查找傳遞給EnquireActivity.java並由TailorAcitivityThree.java發送的值? – rahulserver

回答

1

一個我看到的主要問題是這部分的代碼

i1.putExtra(et_name.getText().toString(), "name"); 
i1.putExtra(et_adult.getText().toString(), "adults"); 
i1.putExtra(et_child.getText().toString(), "child"); 
i1.putExtra(et_email.getText().toString(), "email"); 
i1.putExtra(et_phone.getText().toString(), "phone"); 

getname = extras.getString("name"); 
getadult = extras.getString("adults"); 
getchild = extras.getString("child"); 
getmail = extras.getString("email"); 
getphone = extras.getString("phone"); 

您沒有發送correclty這些值,因爲當您使用putExtra時,第一個參數是鍵,第二個參數是值。這就是爲什麼你無法在第二個活動中檢索它們的原因。

+0

是的,它的工作......非常感謝你 – divyang7

0

意圖包含

意圖I =新意圖名稱 - 值對做糾正在第一活動();

StringBuffer sb = new StringBuffer(「Hello」);

i.putExtra(「email」,「[email protected]」);

i.putExtra(「sub」,sb.toString());

發送這樣的..

發送郵件設置這些參數..

字符串消息= 「Your_message」; String mail_id =「Email_id」;字符串 sub =「郵件主題」;

Intent mail = new Intent(Intent.ACTION_SENDTO,Uri.parse(「mailto:」+ mail_id));

mail.putExtra(Intent.EXTRA_SUBJECT,sub); mail.putExtra(Intent.EXTRA_TEXT,message); (Intent.createChooser(mail,「使用郵件」));

..它會爲你工作..