如何使用从使用纯 java class 数据的片段填充的数据填充电子邮件应用程序?
How to Fill Email app with data populated from a fragment that uses data from a pure java class?
我刚刚开始学习 Android.I 中的片段,现在可以使用 ListFragment 和 ArrayAdapter 在片段中显示数据。现在,我正在尝试在片段上使用 onclickListener 和意图,就像我们处理活动和按钮的方式一样。
我试图在单击列表片段时出现的片段时打开电子邮件应用程序。
但是,我无法获得我想要完成的一件事,那就是联系人的电子邮件地址。我怎样才能实现它?
我已经取得了这些结果,但在第二张图片上,收件人:部分是空的。它应该出现在 fragment.
中的 kurt@nirvana.com
public class ContactDetailsFragment extends Fragment {
private long contactId;
public ContactDetailsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if (savedInstanceState != null){
contactId = savedInstanceState.getLong("contactId");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_contact_details, container, false);
}
@Override
public void onStart() {
super.onStart();
View view = getView();
if (view != null) {
TextView FNameText = (TextView) view.findViewById(R.id.textFName);
final Contact contact = myContact[(int) contactId];
FNameText.setText(contact.getFName());
TextView LNameText = (TextView) view.findViewById(R.id.textLName);
LNameText.setText(contact.getLName());
TextView PhoneText = (TextView) view.findViewById(R.id.textPhone);
PhoneText.setText(contact.getPhone());
final TextView EmailText = (TextView) view.findViewById(R.id.textEmail);
EmailText.setText(contact.getEmail());
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
//intent.setType("text/message");
intent.putExtra(Intent.EXTRA_SUBJECT, "Announcement from Mobile Apps Class");
intent.putExtra(Intent.EXTRA_EMAIL, myContact.toString());
intent.putExtra(Intent.EXTRA_TEXT, "What do you want. How you want? Do you really know what is there in all those Whats?");
startActivity(Intent.createChooser(intent, "Email"));
//Toast.makeText(getActivity(),"Button is clicked!" +contactId, Toast.LENGTH_LONG).show();
}
});
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
savedInstanceState.putLong("contactId", contactId);
}
public void setContact(long id){
this.contactId = id;
}
}
添加这一行
intent.setData(Uri.parse("mailto:"+contact.getEmail()));
并删除
intent.putExtra(Intent.EXTRA_EMAIL, myContact.toString());
解决了我的问题。
我刚刚开始学习 Android.I 中的片段,现在可以使用 ListFragment 和 ArrayAdapter 在片段中显示数据。现在,我正在尝试在片段上使用 onclickListener 和意图,就像我们处理活动和按钮的方式一样。
我试图在单击列表片段时出现的片段时打开电子邮件应用程序。 但是,我无法获得我想要完成的一件事,那就是联系人的电子邮件地址。我怎样才能实现它?
我已经取得了这些结果,但在第二张图片上,收件人:部分是空的。它应该出现在 fragment.
中的 kurt@nirvana.compublic class ContactDetailsFragment extends Fragment {
private long contactId;
public ContactDetailsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if (savedInstanceState != null){
contactId = savedInstanceState.getLong("contactId");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_contact_details, container, false);
}
@Override
public void onStart() {
super.onStart();
View view = getView();
if (view != null) {
TextView FNameText = (TextView) view.findViewById(R.id.textFName);
final Contact contact = myContact[(int) contactId];
FNameText.setText(contact.getFName());
TextView LNameText = (TextView) view.findViewById(R.id.textLName);
LNameText.setText(contact.getLName());
TextView PhoneText = (TextView) view.findViewById(R.id.textPhone);
PhoneText.setText(contact.getPhone());
final TextView EmailText = (TextView) view.findViewById(R.id.textEmail);
EmailText.setText(contact.getEmail());
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
//intent.setType("text/message");
intent.putExtra(Intent.EXTRA_SUBJECT, "Announcement from Mobile Apps Class");
intent.putExtra(Intent.EXTRA_EMAIL, myContact.toString());
intent.putExtra(Intent.EXTRA_TEXT, "What do you want. How you want? Do you really know what is there in all those Whats?");
startActivity(Intent.createChooser(intent, "Email"));
//Toast.makeText(getActivity(),"Button is clicked!" +contactId, Toast.LENGTH_LONG).show();
}
});
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
savedInstanceState.putLong("contactId", contactId);
}
public void setContact(long id){
this.contactId = id;
}
}
添加这一行
intent.setData(Uri.parse("mailto:"+contact.getEmail()));
并删除
intent.putExtra(Intent.EXTRA_EMAIL, myContact.toString());
解决了我的问题。