-
-
Save rafaeltoledo/ea55071db3f6943de34105700f756bef to your computer and use it in GitHub Desktop.
Android 06
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.rafaeltoledo.restaurante; | |
import java.util.ArrayList; | |
import java.util.List; | |
import net.rafaeltoledo.restaurante.model.Restaurante; | |
import android.app.TabActivity; | |
import android.os.Bundle; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.view.ViewGroup; | |
import android.widget.AdapterView.OnItemClickListener; | |
import android.widget.AdapterView; | |
import android.widget.ArrayAdapter; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.ImageView; | |
import android.widget.ListView; | |
import android.widget.RadioGroup; | |
import android.widget.TabHost.TabSpec; | |
import android.widget.TextView; | |
public class ListaRestaurantes extends TabActivity { | |
List<Restaurante> listaRestaurantes = new ArrayList<Restaurante>(); | |
AdaptadorRestaurante adaptador = null; | |
EditText nome = null; | |
EditText endereco = null; | |
RadioGroup tipos = null; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
nome = (EditText) findViewById(R.id.nome); | |
endereco = (EditText) findViewById(R.id.end); | |
tipos = (RadioGroup) findViewById(R.id.tipos); | |
Button salvar = (Button) findViewById(R.id.salvar); | |
salvar.setOnClickListener(onSave); | |
ListView lista = (ListView) findViewById(R.id.restaurantes); | |
adaptador = new AdaptadorRestaurante(); | |
lista.setAdapter(adaptador); | |
lista.setOnItemClickListener(onListClick); | |
TabSpec descritor = getTabHost().newTabSpec("tag1"); | |
descritor.setContent(R.id.restaurantes); | |
descritor.setIndicator("Lista", getResources().getDrawable(R.drawable.lista)); | |
getTabHost().addTab(descritor); | |
descritor = getTabHost().newTabSpec("tag2"); | |
descritor.setContent(R.id.detalhes); | |
descritor.setIndicator("Detalhes", getResources().getDrawable(R.drawable.restaurante)); | |
getTabHost().addTab(descritor); | |
getTabHost().setCurrentTab(0); | |
} | |
private OnItemClickListener onListClick = new OnItemClickListener() { | |
public void onItemClick(AdapterView<?> parent, View view, int position, | |
long id) { | |
Restaurante r = listaRestaurantes.get(position); | |
nome.setText(r.getNome()); | |
endereco.setText(r.getEndereco()); | |
if (r.getTipo().equals("rodizio")) { | |
tipos.check(R.id.rodizio); | |
} else if (r.getTipo().equals("fast_food")) { | |
tipos.check(R.id.fast_food); | |
} else { | |
tipos.check(R.id.a_domicilio); | |
} | |
getTabHost().setCurrentTab(1); | |
} | |
}; | |
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); | |
} | |
}; | |
class AdaptadorRestaurante extends ArrayAdapter<Restaurante> { | |
AdaptadorRestaurante() { | |
super(ListaRestaurantes.this, R.layout.linha, | |
listaRestaurantes); | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
View linha = convertView; | |
ArmazenadorRestaurante armazenador = null; | |
if (linha == null) { | |
LayoutInflater inflater = getLayoutInflater(); | |
linha = inflater.inflate(R.layout.linha, parent, false); | |
armazenador = new ArmazenadorRestaurante(linha); | |
linha.setTag(armazenador); | |
} else { | |
armazenador = (ArmazenadorRestaurante) linha.getTag(); | |
} | |
armazenador.popularFormulario(listaRestaurantes.get(position)); | |
return linha; | |
} | |
} | |
static class ArmazenadorRestaurante { | |
private TextView nome = null; | |
private TextView endereco = null; | |
private ImageView icone = null; | |
ArmazenadorRestaurante(View linha) { | |
nome = (TextView) linha.findViewById(R.id.titulo); | |
endereco = (TextView) linha.findViewById(R.id.endereco); | |
icone = (ImageView) linha.findViewById(R.id.icone); | |
} | |
void popularFormulario(Restaurante r) { | |
nome.setText(r.getNome()); | |
endereco.setText(r.getEndereco()); | |
if (r.getTipo().equals("rodizio")) { | |
icone.setImageResource(R.drawable.rodizio); | |
} else if (r.getTipo().equals("fast_food")) { | |
icone.setImageResource(R.drawable.fast_food); | |
} else { | |
icone.setImageResource(R.drawable.entrega); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@android:id/tabhost" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent"> | |
<LinearLayout | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:orientation="vertical"> | |
<TabWidget | |
android:id="@android:id/tabs" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" /> | |
<FrameLayout | |
android:id="@android:id/tabcontent" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent"> | |
<ListView | |
android:id="@+id/restaurantes" | |
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:stretchColumns="1" | |
android:paddingTop="4dip"> | |
<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> | |
</FrameLayout> | |
</LinearLayout> | |
</TabHost> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ListaRestaurantes extends TabActivity { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TabSpec descritor = getTabHost().newTabSpec("tag1"); | |
descritor.setContent(R.id.restaurantes); | |
descritor.setIndicator("Lista", getResources().getDrawable(R.drawable.lista)); | |
getTabHost().addTab(descritor); | |
descritor = getTabHost().newTabSpec("tag2"); | |
descritor.setContent(R.id.detalhes); | |
descritor.setIndicator("Detalhes", getResources().getDrawable(R.drawable.restaurante)); | |
getTabHost().addTab(descritor); | |
getTabHost().setCurrentTab(0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private OnItemClickListener onListClick = new OnItemClickListener() { | |
public void onItemClick(AdapterView<?> parent, View view, int position, | |
long id) { | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lista.setOnItemClickListener(onListClick); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EditText nome = null; | |
EditText endereco = null; | |
RadioGroup tipos = null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nome = (EditText) findViewById(R.id.nome); | |
endereco = (EditText) findViewById(R.id.end); | |
tipos = (RadioGroup) findViewById(R.id.tipos); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private OnItemClickListener onListClick = new OnItemClickListener() { | |
public void onItemClick(AdapterView<?> parent, View view, int position, | |
long id) { | |
Restaurante r = listaRestaurantes.get(position); | |
nome.setText(r.getNome()); | |
endereco.setText(r.getEndereco()); | |
if (r.getTipo().equals("rodizio")) { | |
tipos.check(R.id.rodizio); | |
} else if (r.getTipo().equals("fast_food")) { | |
tipos.check(R.id.fast_food); | |
} else { | |
tipos.check(R.id.a_domicilio); | |
} | |
} | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getTabHost().setCurrentTab(1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment