-
-
Save rafaeltoledo/ff0fbdb59184c3e3802f1583150a023d to your computer and use it in GitHub Desktop.
Android 09
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 java.util.concurrent.atomic.AtomicBoolean; | |
import net.rafaeltoledo.restaurante.model.Restaurante; | |
import android.app.TabActivity; | |
import android.os.Bundle; | |
import android.os.SystemClock; | |
import android.view.LayoutInflater; | |
import android.view.Menu; | |
import android.view.MenuInflater; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.view.ViewGroup; | |
import android.view.Window; | |
import android.widget.AdapterView; | |
import android.widget.AdapterView.OnItemClickListener; | |
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.Toast; | |
import android.widget.TabHost.TabSpec; | |
import android.widget.TextView; | |
public class ListaRestaurantes extends TabActivity { | |
List<Restaurante> listaRestaurantes = new ArrayList<Restaurante>(); | |
AdaptadorRestaurante adaptador = null; | |
Restaurante atual = null; | |
EditText nome = null; | |
EditText endereco = null; | |
EditText anotacoes = null; | |
RadioGroup tipos = null; | |
int progresso = 0; | |
AtomicBoolean estaAtivo = new AtomicBoolean(true); | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
requestWindowFeature(Window.FEATURE_PROGRESS); | |
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 void fazerAlgoDemorado(final int incremento) { | |
runOnUiThread(new Runnable() { | |
public void run() { | |
progresso += incremento; | |
setProgress(progresso); | |
} | |
}); | |
SystemClock.sleep(250); // Pode ser algo mais útil! | |
} | |
private Runnable tarefaLonga = new Runnable() { | |
public void run() { | |
for (int i = progresso; i < 10000 && estaAtivo.get(); i += 200) { | |
fazerAlgoDemorado(500); | |
} | |
if (estaAtivo.get()) { | |
runOnUiThread(new Runnable() { | |
public void run() { | |
setProgressBarVisibility(false); | |
progresso = 0; | |
} | |
}); | |
} | |
} | |
}; | |
@Override | |
public void onPause() { | |
super.onPause(); | |
estaAtivo.set(false); | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
estaAtivo.set(true); | |
if (progresso > 0) { | |
iniciarTarefa(); | |
} | |
} | |
private void iniciarTarefa() { | |
setProgressBarVisibility(true); | |
new Thread(tarefaLonga).start(); | |
} | |
private OnItemClickListener onListClick = new OnItemClickListener() { | |
public void onItemClick(AdapterView<?> parent, View view, int position, | |
long id) { | |
atual = listaRestaurantes.get(position); | |
nome.setText(atual.getNome()); | |
endereco.setText(atual.getEndereco()); | |
anotacoes.setText(atual.getAnotacoes()); | |
if (atual.getTipo().equals("rodizio")) { | |
tipos.check(R.id.rodizio); | |
} else if (atual.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) { | |
atual = new Restaurante(); | |
EditText nome = (EditText) findViewById(R.id.nome); | |
EditText endereco = (EditText) findViewById(R.id.end); | |
EditText anotacoes = (EditText) findViewById(R.id.anotacoes); | |
atual.setNome(nome.getText().toString()); | |
atual.setEndereco(endereco.getText().toString()); | |
atual.setAnotacoes(anotacoes.getText().toString()); | |
RadioGroup tipos = (RadioGroup) findViewById(R.id.tipos); | |
switch (tipos.getCheckedRadioButtonId()) { | |
case R.id.rodizio: | |
atual.setTipo("rodizio"); | |
break; | |
case R.id.fast_food: | |
atual.setTipo("fast_food"); | |
break; | |
case R.id.a_domicilio: | |
atual.setTipo("a_domicilio"); | |
break; | |
} | |
adaptador.add(atual); | |
} | |
}; | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
new MenuInflater(this).inflate(R.menu.opcao, menu); | |
return super.onCreateOptionsMenu(menu); | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
if (item.getItemId() == R.id.anotacao) { | |
String mensagem = "Nenhum restaurante selecionado"; | |
if (atual != null) { | |
mensagem = atual.getAnotacoes(); | |
} | |
Toast.makeText(this, mensagem, Toast.LENGTH_LONG).show(); | |
return true; | |
} else if (item.getItemId() == R.id.rodar) { | |
iniciarTarefa(); | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
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
private Runnable tarefaLonga = new Runnable() { | |
public void run() { | |
for (int i = progresso; i < 10000; i += 200) { | |
fazerAlgoDemorado(500); | |
} | |
runOnUiThread(new Runnable() { | |
public void run() { | |
setProgressBarVisibility(false); | |
} | |
}); | |
} | |
}; |
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; | |
EditText anotacoes = null; | |
RadioGroup tipos = null; | |
int progresso = 0; | |
AtomicBoolean estaAtivo = new AtomicBoolean(true); |
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
for (int i = progresso; i < 10000 && estaAtivo.get(); i += 200) { | |
fazerAlgoDemorado(500); | |
} |
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
@Override | |
public void onPause() { | |
super.onPause(); | |
estaAtivo.set(false); | |
} |
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
@Override | |
public void onResume() { | |
super.onResume(); | |
estaAtivo.set(true); | |
if (progresso > 0) { | |
iniciarTarefa(); | |
} | |
} |
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 void iniciarTarefa() { | |
setProgressBarVisibility(true); | |
new Thread(tarefaLonga).start(); | |
} |
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
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
if (item.getItemId() == R.id.anotacao) { | |
String mensagem = "Nenhum restaurante selecionado"; | |
if (atual != null) { | |
mensagem = atual.getAnotacoes(); | |
} | |
Toast.makeText(this, mensagem, Toast.LENGTH_LONG).show(); | |
return true; | |
} else if (item.getItemId() == R.id.rodar) { | |
iniciarTarefa(); | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} |
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 Runnable tarefaLonga = new Runnable() { | |
public void run() { | |
for (int i = progresso; i < 10000 && estaAtivo.get(); i += 200) { | |
fazerAlgoDemorado(500); | |
} | |
if (estaAtivo.get()) { | |
runOnUiThread(new Runnable() { | |
public void run() { | |
setProgressBarVisibility(false); | |
progresso = 0; | |
} | |
}); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment