How to create a new window for each button clicked?

Asked

Viewed 137 times

3

Hello, I’m beginner in programming for android, and I’m making an application (course work IT).

The first Activity has 12 buttons with a "table" background, representing the tables of a restaurant. By clicking, you should open a window to enter customer data from the respective table.

Example: People (Quantity), orders to be made, total price. Only I have 12 Buttons (tables), I would have to create 12 Activity with Layouts for each table? I think this would weigh in. Is there any way to make it better ?

inserir a descrição da imagem aqui

(After you have informed the table data, you will confirm in this single window of each table, and the table will change from the background to an image of the gray table, indicating that you already have client.)

  • All you need is an Activity or, possibly better, a Dialogfragment.

  • Understood, but if I come back from this window, example, table 1 window, click table 2, insert the dice into it, table 1 will not get lost ne ? 'Cause that can’t happen.

  • You will need to create a system to store the data of each table, it can be a simple Arraylist or a database.

1 answer

3


You can solve this with a dialog.

When creating the dialog you need to define a layout in which you will be asked for the information you want to request regarding each table. Below is an example that can help you solve your problem. dialog_layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">

<EditText
    android:id="@+id/value"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="4dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginTop="16dp"
    android:hint="Valor"
    android:inputType="numberDecimal" /></LinearLayout>

In the class or Fragment in which you want to inflate the dialog you must add the following code:

final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            // Get the layout inflater
            final LayoutInflater inflater = getActivity().getLayoutInflater();
            final View v = inflater.inflate(R.layout.dialog_layout, null);
            // Inflate and set the layout for the dialog
            // Pass null as the parent view because its going in the dialog layout
            builder.setView(v)
                    // Add action buttons
                    .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                           String valorDigitado = ((EditText) v).getText().toString();
                            showToast();
                        }
                    })
                    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            AlertDialog dialog = builder.create();
            dialog.show();
            break;

When clicked on confirm(setPositiveButton) you must fetch all the data that has been filled in;

To save your information you can create an Arraylist of the Table class as follows:

ArrayList<Mesa> mesa = new ArrayList<Mesa>()

So every time one is clicked on to confirm you should create a table object and add it, suppose table has the total fields and tip then we would have:

public class Mesa{
private String valor;
private String gorjeta;
public String getGorjeta(){
  return this.gorjeta;
}
public void setGorjeta(String gorjeta){
   this.gorjeta = gorjeta;
}
public String getValor(){
  return this.valor;
}
public void setValor(String valor){
   this.valor = valor;
}

}

So here’s what you’d do:

Mesa m = new Mesa();
m.setValor("valor que sera obtido do dialog como no exemplo dessa resposta");
m.setGorjeta("valor que sera obtido do dialog como no exemplo dessa resposta");
mesa.add(posicaodoClick,m);

the value posicaClick you get through the setOnItemClickListener method that will give you the position that was clicked, this is necessary for you to keep track of which position was clicked, I believe you are using a gridView then it would be something like:

 gridview.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v,
            int position, long id) {
       //chama dialog;
       //preenche objeto do tipo mesa
       //insere no ArrayList de Mesa
       //mesa.add(position,objetoTipoMesa);
    }
});
  • 1

    All that’s missing is the way to share ArrayList<Mesa> between the Activity and the Dialog

  • I still don’t understand MUCH about it, but you gave a perfect explanation, man. I’m starting now, and sometimes things don’t get clear to me. Thank you so much, man. Exactly what I wanted =)

  • 1

    @Nilzonmartins Good afternoon, if the answer solved your problem, please mark it as correct. If you do not know how to do it then read this: http://meta.pt.stackoverflow.com/q/1078/3635 - Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.