好的,一个简单的问题:我在主要活动中有一个数组列表。当用户单击一个按钮时,我让它打开一个对话框,用户可以在其中输入姓名、电话号码和电子邮件地址,当他们单击对话框按钮“添加联系人”时,他们的输入存储在上述数组列表中。如何让它显示在第二个对话框中(即在 textview 等中)?
我的 MainActivity.java 代码:package com.example.java2midterm_lefelhocz;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.app.DialogFragment;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity {
@SuppressWarnings("unused")
private Button btnAdd;
@SuppressWarnings("unused")
private Button btnView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void btnAddOnClick(View v){
DialogFragment newFragment = new DialogFragment();
newFragment.show(getFragmentManager(), "Add A Contact");
}
ArrayList <String>contacts = new ArrayList<String>();
public void btnViewOnClick(View v){
DialogFragment newFragment = new DialogFragment();
newFragment.show(getFragmentManager(), "View Contacts");
}
}
AddContactsDialog:包 com.example.java2midterm_lefelhocz;
import android.app.DialogFragment;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.*;
public class AddContactDialog extends DialogFragment {
private EditText txtName;
private EditText txtPhone;
private EditText txtEmail;
private LayoutInflater inflater;
public AlertDialog onCreateDialog(Bundle savedInstanceState){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.add_contacts, null));
builder.setTitle("Add A Contact");
builder.setPositiveButton("AddContact", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MainActivity ma = (MainActivity)getActivity();
txtName = (EditText)AddContactDialog.this.getDialog().findViewById(R.id.txtName);
txtPhone = (EditText)AddContactDialog.this.getDialog().findViewById(R.id.txtPhone);
txtEmail = (EditText)AddContactDialog.this.getDialog().findViewById(R.id.txtEmail);
ma.contacts.add(txtName.getText().toString());
ma.contacts.add(txtPhone.getText().toString());
ma.contacts.add(txtEmail.getText().toString());
}
});
return builder.create();
}
}
最后,ViewContacts.java:
包 com.example.java2midterm_lefelhocz;
import android.app.DialogFragment;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.*;
public class ViewContacts extends DialogFragment {
private TextView txtContacts;
private LayoutInflater inflater;
public AlertDialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.view_contacts, null));
builder.setTitle("Contacts");
return builder.create();
}
}