How to create a new window for each click button? [closed]

3

Hello, I'm a beginner in programming for android, and I'm making an application (work from the TI course).

It is as follows: The first Activity has 12 buttons with background of "tables", representing the tables of a restaurant. When clicking, you should open a window to enter the customer data of the respective table.

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

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

    
asked by anonymous 21.11.2015 / 15:47

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 prompted for the information you want to request regarding each table. Here 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 where you want to inflate the dialog you should 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 you click on confirm (setPositiveButton) you should fetch all the data that has been filled;

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

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

So whenever a click is clicked on confirm you should create a table object and add it, suppose that table has the total and tip fields 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;
}

}

Then you would do the following:

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 position valueClick you get through the method setOnItemClickListener 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 so 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);
    }
});
    
22.11.2015 / 05:16