0

好的,一个简单的问题:我在主要活动中有一个数组列表。当用户单击一个按钮时,我让它打开一个对话框,用户可以在其中输入姓名、电话号码和电子邮件地址,当他们单击对话框按钮“添加联系人”时,他们的输入存储在上述数组列表中。如何让它显示在第二个对话框中(即在 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();
}
}
4

1 回答 1

0

您可以通过以下方式传入contactsBundle:

newFragment.setArguments(bundle);

getArguments()然后在您的片段中阅读它们。或者,您可以contacts像在 AddContactsDialog 中一样将其作为静态变量进行读取。使用public变量很方便,但通常被认为是不安全的,首选方法是使用 getter / setter 和其他间接传递数据的方式(如在 Bundle 中)。

您可以通过多种方式在对话框中显示这些联系人,最基本的是setItems()

OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Log.v("Dialog List", "You clicked item " + which);
    }
};

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());     
builder.setTitle("Contacts");
builder.setItems(MainActivity.contacts, dialogClickListener);
return builder.create();
于 2013-03-07T16:48:26.360 回答