打开对话框并在textview中显示arraylist

时间:2013-03-07 16:29:40

标签: android arraylist dialog android-dialogfragment

好的,一个简单的问题:我在主要活动中有一个arraylist。当用户点击一个按钮时,我打开一个对话框,用户可以在其中输入姓名,电话号码和电子邮件地址,当他们点击对话框按钮“添加联系人”时,他们的输入存储在前面提到的arraylist中。如何在第二个对话框(即文本视图等)中显示它?

我的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:     package 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:

package 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();
}
}

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式将contacts传递给捆绑包:

newFragment.setArguments(bundle);

然后在片段中使用getArguments()阅读它们。或者您可以像在AddContactsDialog中将其作为静态变量一样阅读contacts。使用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();