如何使用Accept / Cancel和MainActivity中的数据创建自定义对话框?

时间:2014-12-16 22:13:32

标签: android database dialog android-alertdialog

我想从我的MainActivity创建一个Dialog,带有一个Accept和Cancel按钮,它有一个自定义视图(我创建了它,一个布局)。在那个自定义对话框中,我需要填充一个我给他的ArrayList(我不知道怎么做btw)的旋转器,我希望用户选择这两个微调器的选项,需要点击"接受"当用户点击"接受"我必须将他的创作添加到我的数据库中。

我完全迷失了,我只创建了这个布局,这是对话框的视图,我不知道如何做到这一切......有人请帮助我,这是非常的令人沮丧=(

这是对话框的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/dialog_evento"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Spinner
        android:id="@+id/spinnerEvento"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/dialog_accion"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Spinner
        android:id="@+id/spinnerAccion"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

这是集体诉讼:

package com.nahue.actions;

public class Action {

  //declaración de atributos
  private int Id;
  public int getId() {
    return Id;
}

public void setId(int id) {
    Id = id;
}

public int getIdAccion() {
    return IdAccion;
}

public void setIdAccion(int idAccion) {
    IdAccion = idAccion;
}

public int getIdEvento() {
    return IdEvento;
}

public void setIdEvento(int idEvento) {
    IdEvento = idEvento;
}

public boolean getActiva() {
    return Activa;
}

public void setActiva(boolean activa) {
    Activa = activa;
}

private int IdAccion;
  private int IdEvento;
  private boolean Activa;

  //declaración de constructor
  public Action(int Id, int IdAccion, int IdEvento, boolean Activa){
    this.Id = Id; //Autonumérico
    this.IdAccion = IdAccion;
    this.IdEvento = IdEvento;
    this.Activa = Activa;
  }
}

这是DialogActions类:

package com.nahue.actions;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.app.DialogFragment;
// ...
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import com.nahue.actions.R;

public class DialogActions extends DialogFragment
{    



    private Button cancelButton;
    private Button confirmButton;
    private DialogActions DialogListener;
    private Spinner spinnerAccion;
    private Spinner spinnerEvento;

    public DialogActions()
    {
        // Empty constructor required for DialogFragment
    }

    //This is how you can supply your fragment with information
    public static DialogActions newInstance(ArrayList<Action> ListaActions)
    {
        DialogActions myDialog = new DialogActions();
        Bundle args = new Bundle();
        args.putParcelableArrayList("ListaActions", ArrayList<Action> ListaActions);//Errors: ArrayList and Action cannot be resolved into variables
        myDialog.setArguments(args);
        return myDialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_TITLE, 0); // remove title from dialogfragment
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.dialog_actions, container);

        //DECLARO LOS ELEMENTOS EN EL LAYOUT


        //Setup cancel button listener
        cancelButton = (Button) view.findViewById(R.id.cancelButton);
        cancelButton.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                getDialog().dismiss();
            }
        });

        //Setup confirm button listener
        confirmButton = (Button) view.findViewById(R.id.confirmButton);
        confirmButton.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //AGREGAR A LA BD
                getDialog().dismiss();
            }
        });
        return view;
    }

    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        if (activity instanceof DialogActions) //Error: Incompatible conditional operand types Activity and DialogActions
        {
            DialogListener = (DialogActions) activity; //Error: "Cannot cast from Activity to DialogActions"
        }
        else
        {
            throw new ClassCastException(activity.toString() + " must implement StartProfileDialog.StartProfileListener");
        }
    }
}

3 个答案:

答案 0 :(得分:3)

有很多方法可以做到这一点,每个方法都有自己的专业和缺点。我目前这样做的方法是使每个自定义对话框成为DialogFragment。

这是班级

import android.app.Activity;
import android.os.Bundle;
import android.app.DialogFragment;
// ...
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;


public class StartProfileDialog extends DialogFragment
{    

    public interface StartProfileListener
    {
        void onStartProfile();
    }

    private Button cancelButton;
    private Button confirmButton;
    private StartProfileListener startProfileListener;
    private String profileName;

    public StartProfileDialog()
    {
        // Empty constructor required for DialogFragment
    }

    //This is how you can supply your fragment with information
    public static StartProfileDialog newInstance(String profileName)
    {
        StartProfileDialog myDialog = new StartProfileDialog();
        Bundle args = new Bundle();
        args.putString("profileName", profileName);
        myDialog.setArguments(args);
        return myDialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_TITLE, 0); // remove title from dialogfragment
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_start_profile_dialog, container);
        profileName = getArguments().getString("profileName"); 

        //Set Text in center
        TextView startProfileMessage = (TextView) view.findViewById(R.id.startProfileMessage);
        String startProfileMessageContent = getString(R.string.start_profile_prefix) + " " + profileName
            + " " + getString(R.string.start_profile_suffix);
        startProfileMessage.setText(startProfileMessageContent);

        //Setup cancel button listener
        cancelButton = (Button) view.findViewById(R.id.cancelButton);
        cancelButton.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                getDialog().dismiss();
            }
        });

        //Setup confirm button listener
        confirmButton = (Button) view.findViewById(R.id.confirmButton);
        confirmButton.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                startProfileListener.onStartProfile();
                getDialog().dismiss();
            }
        });
        return view;
    }

    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        if (activity instanceof StartProfileListener)
        {
            startProfileListener = (StartProfileListener) activity;
        }
        else
        {
            throw new ClassCastException(activity.toString() + " must implement StartProfileDialog.StartProfileListener");
        }
    }
}

和XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/startProfileDialogTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/start_profile"
        android:textSize="24sp"
        android:layout_gravity="center"" />
    <TextView
        android:id="@+id/startProfileMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="horizontal"
    />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal"
    >
        <Button
            android:id="@+id/cancelButton"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="@string/cancel"
        />
        <Button
            android:id="@+id/confirmButton"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="@string/confirm"
        />
    </LinearLayout>
</LinearLayout>

用法:

public class ProfileViewActivity extends Activity implements StartProfileListener
{
    //...
    public void startStopProfileButtonPressed (View v)
    {
        FragmentManager fm = getFragmentManager();
        StartProfileDialog profileWarningDialog = StartProfileDialog.newInstance(mProfile.name);
        profileWarningDialog.show(fm, "fragment_start_profile_dialog");
    }

    //...
    @Override
    public void onStartProfile()
    {
        //.. Do stuff here, you can supply parameters for any data you need from the spinners and use them in your activity.
    }
}

要回答您的问题,您需要在newInstance函数中提供ArrayList,并且您要在侦听器函数中的微调器中传递选定的值。

答案 1 :(得分:0)

您可以使用对话框,但由于对话框中的大信息屏幕尺寸很小,我使用了一个活动。

您可以使用主要活动中的Intent对象参数将其设为活动并传递输入。当用户点击取消时 - 您可能希望返回主要活动。对accept的处理可以作为此活动中按钮的单击处理程序处理。

答案 2 :(得分:0)

您可以使用DialogFragment,查看示例here