Skip to content

Instantly share code, notes, and snippets.

@rafaeltoledo

rafaeltoledo/ListaRestaurantes.java Secret

Last active Oct 26, 2019
Embed
What would you like to do?
Android 04
package net.rafaeltoledo.restaurante;
import java.util.ArrayList;
import java.util.List;
import net.rafaeltoledo.restaurante.model.Restaurante;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
public class ListaRestaurantes extends Activity {
List<Restaurante> listaRestaurantes = new ArrayList<Restaurante>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button salvar = (Button) findViewById(R.id.salvar);
salvar.setOnClickListener(onSave);
}
private OnClickListener onSave = new OnClickListener() {
public void onClick(View arg0) {
Restaurante r = new Restaurante();
EditText nome = (EditText) findViewById(R.id.nome);
EditText endereco = (EditText) findViewById(R.id.end);
r.setNome(nome.getText().toString());
r.setEndereco(endereco.getText().toString());
RadioGroup tipos = (RadioGroup) findViewById(R.id.tipos);
switch (tipos.getCheckedRadioButtonId()) {
case R.id.rodizio:
r.setTipo("rodizio");
break;
case R.id.fast_food:
r.setTipo("fast_food");
break;
case R.id.a_domicilio:
r.setTipo("a_domicilio");
break;
}
listaRestaurantes.add(r);
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:id="@+id/detalhes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:stretchColumns="1">
<TableRow>
<TextView android:text="Nome:" />
<EditText android:id="@+id/nome" />
</TableRow>
<TableRow>
<TextView android:text="Endereço:" />
<EditText android:id="@+id/end" />
</TableRow>
<TableRow>
<TextView android:text="Tipo:" />
<RadioGroup android:id="@+id/tipos">
<RadioButton
android:id="@+id/rodizio"
android:text="Rodízio" />
<RadioButton
android:id="@+id/fast_food"
android:text="Fast Food" />
<RadioButton
android:id="@+id/a_domicilio"
android:text="A Domicílio" />
</RadioGroup>
</TableRow>
<Button
android:id="@+id/salvar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Salvar" />
</TableLayout>
<ListView
android:id="@+id/restaurantes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="@id/detalhes" />
</RelativeLayout>
package net.rafaeltoledo.restaurante;
import java.util.ArrayList;
import java.util.List;
import net.rafaeltoledo.restaurante.model.Restaurante;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;
public class ListaRestaurantes extends Activity {
List<Restaurante> listaRestaurantes = new ArrayList<Restaurante>();
ArrayAdapter<Restaurante> adaptador = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button salvar = (Button) findViewById(R.id.salvar);
salvar.setOnClickListener(onSave);
ListView lista = (ListView) findViewById(R.id.restaurantes);
adaptador = new ArrayAdapter<Restaurante>(this,
android.R.layout.simple_list_item_1, listaRestaurantes);
lista.setAdapter(adaptador);
}
private OnClickListener onSave = new OnClickListener() {
public void onClick(View arg0) {
Restaurante r = new Restaurante();
EditText nome = (EditText) findViewById(R.id.nome);
EditText endereco = (EditText) findViewById(R.id.end);
r.setNome(nome.getText().toString());
r.setEndereco(endereco.getText().toString());
RadioGroup tipos = (RadioGroup) findViewById(R.id.tipos);
switch (tipos.getCheckedRadioButtonId()) {
case R.id.rodizio:
r.setTipo("rodizio");
break;
case R.id.fast_food:
r.setTipo("fast_food");
break;
case R.id.a_domicilio:
r.setTipo("a_domicilio");
break;
}
adaptador.add(r);
}
};
}
package net.rafaeltoledo.restaurante.model;
public class Restaurante {
private String nome = "";
private String endereco = "";
private String tipo = "";
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getEndereco() {
return endereco;
}
public void setEndereco(String endereco) {
this.endereco = endereco;
}
public String getTipo() {
return tipo;
}
public void setTipo(String tipo) {
this.tipo = tipo;
}
@Override
public String toString() {
return getNome();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.