I want to create a Dialog from my MainActivity, with an Accept and Cancel button, that has a custom view (I have it created, a layout). In that custom dialog there're 2 spinners that I have to fill with an ArrayList that I'm giving to him (I don't know how to do that btw), and I want the user to select options from those two spinners, make them required for clicking "Accept" and when the user clicks "Accept" I have to add his creation to my database.
I'm completely lost, I only have this layout created, that is the view for the dialog, and I don't know how to do all this... Somebody please help me, this is very frustrating =(
This is the XML for the Dialog:
<?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>
This is the Class Action:
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;
}
}
And this is the DialogActions class:
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");
}
}
}