aula android 04
DESCRIPTION
Quarta aula de Android. Apresentação sobre: -Listagem simples e complexa. -Trabalhando com relacionamentos com o ORMLite -Utilizando a câmeraTRANSCRIPT
![Page 1: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/1.jpg)
sábado, 10 de novembro de 12
![Page 2: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/2.jpg)
Desenvolvedor de softwarewww.carloscavalcanti.com
twitter @carloscaval
github.com/carloscavalcanti
slideshare.net/carloscavalcanti
sábado, 10 de novembro de 12
![Page 3: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/3.jpg)
sábado, 10 de novembro de 12
![Page 4: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/4.jpg)
sábado, 10 de novembro de 12
![Page 5: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/5.jpg)
Desenvolvedor de 17 anos cria app para iPhone e recebe US$ 1 milhão
Summly é um app para iOS que oferece um leitor de notícias que permite navegar mais rápido. Através de sua tecnologia de inteligência artificial, ele oferece resumos concisos do conteúdo online.
sábado, 10 de novembro de 12
![Page 6: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/6.jpg)
ORMLite
Câmera
Listagem
sábado, 10 de novembro de 12
![Page 7: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/7.jpg)
sábado, 10 de novembro de 12
![Page 8: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/8.jpg)
Criando uma lista simples
sábado, 10 de novembro de 12
![Page 9: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/9.jpg)
O que é necessário?
sábado, 10 de novembro de 12
![Page 10: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/10.jpg)
O que é necessário?
Classe ListActivity
Arquivo XML para exibir conteúdo da linha
Objeto Adapter
sábado, 10 de novembro de 12
![Page 11: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/11.jpg)
Exemplopublic class MainActivity extends ListActivity {
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String nomes[] = new String[]{"João", "Maria", "Cristina", "Marcos", "Eric", "Tâmara", "Joana", "Junior", "Bruna", "André"}; ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.activity_main, nomes); setListAdapter(arrayAdapter); }
}
sábado, 10 de novembro de 12
![Page 12: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/12.jpg)
Ao clicar em um item fazer alguma ação
sábado, 10 de novembro de 12
![Page 13: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/13.jpg)
Evento click
ListView listView = getListView();listView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(), Toast.LENGTH_SHORT).show();
}});
sábado, 10 de novembro de 12
![Page 14: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/14.jpg)
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String nomes[] = new String[]{"João", "Maria", "Cristina", "Marcos",
"Eric", "Tâmara", "Joana", "Junior", "Bruna", "André"}; ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.activity_main,
nomes); setListAdapter(arrayAdapter); ListView listView = getListView(); listView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) { Toast.makeText(getApplicationContext(), ((TextView)
arg1).getText(), Toast.LENGTH_SHORT).show(); } });
}
Código
sábado, 10 de novembro de 12
![Page 15: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/15.jpg)
sábado, 10 de novembro de 12
![Page 16: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/16.jpg)
Listagem complexa
sábado, 10 de novembro de 12
![Page 17: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/17.jpg)
Vamos imaginar uma classe
Cliente. E vamos exibir
uma lista de clientes
sábado, 10 de novembro de 12
![Page 18: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/18.jpg)
Classe Cliente
public class Cliente { private String nome; private Date dataNascimento;
sábado, 10 de novembro de 12
![Page 19: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/19.jpg)
Layout
sábado, 10 de novembro de 12
![Page 20: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/20.jpg)
Como eu quero exibir os dados?
sábado, 10 de novembro de 12
![Page 21: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/21.jpg)
Precisamos criar um layout para representar
uma linha da nossa listagem
sábado, 10 de novembro de 12
![Page 22: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/22.jpg)
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayoutItem" android:layout_height="wrap_content" android:layout_width="wrap_content" android:gravity="left|center" android:paddingBottom="5sp" android:paddingTop="5sp" android:paddingLeft="5sp"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/txtNome" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="Nome" android:textSize="20sp" android:layout_marginLeft="15sp"/> <TextView android:id="@+id/txtDataNascimento" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="01/01/1999" android:textSize="20sp" android:layout_marginLeft="20sp" /> </LinearLayout></LinearLayout>
sábado, 10 de novembro de 12
![Page 23: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/23.jpg)
E agora precisamos de uma lista
sábado, 10 de novembro de 12
![Page 24: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/24.jpg)
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/base" android:layout_width="fill_parent" android:layout_height="fill_parent" >
<ListView android:id="@+id/android:list" android:layout_width="fill_parent" android:layout_height="fill_parent" />
</LinearLayout>
sábado, 10 de novembro de 12
![Page 25: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/25.jpg)
sábado, 10 de novembro de 12
![Page 26: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/26.jpg)
Será necessário um cara pra organizar nossa lista
sábado, 10 de novembro de 12
![Page 27: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/27.jpg)
Será necessário um cara pra organizar nossa lista
sábado, 10 de novembro de 12
![Page 28: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/28.jpg)
Adapter
sábado, 10 de novembro de 12
![Page 29: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/29.jpg)
sábado, 10 de novembro de 12
![Page 30: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/30.jpg)
Extender de BaseAdapter
sábado, 10 de novembro de 12
![Page 31: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/31.jpg)
Vamos ver como fica
sábado, 10 de novembro de 12
![Page 32: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/32.jpg)
public class ClienteAdapter extends BaseAdapter { private List<Cliente> clientes; private LayoutInflater mInflater; private ViewHolder holder;
static class ViewHolder{ private TextView txtNome; private TextView txtDataNascimento; }
public ClienteAdapter(Context context, List<Cliente> cliente) { mInflater = LayoutInflater.from(context); this.clientes = cliente; }
@Override public int getCount() { return clientes.size(); }
@Override public Object getItem(int index) { return clientes.get(index); }
@Override public long getItemId(int index) { return index; }sábado, 10 de novembro de 12
![Page 33: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/33.jpg)
ViewHolder&
LayoutInflate
sábado, 10 de novembro de 12
![Page 34: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/34.jpg)
@Override public View getView(int posicao, View convertView, ViewGroup arg2) {
if (convertView == null) { convertView = mInflater.inflate(R.layout.cliente_list, null); holder = new ViewHolder();
holder.txtNome = (TextView) convertView.findViewById(R.id.txtNome); holder.txtDataNascimento = (TextView) convertView .findViewById(R.id.txtDataNascimento);
convertView.setTag(holder);
} else { holder = (ViewHolder) convertView.getTag(); }
Cliente cliente = clientes.get(posicao); holder.txtNome.setText(cliente.getNome()); holder.txtDataNascimento.setText(cliente.getDataNascimento().toGMTString());
return convertView; }
}
sábado, 10 de novembro de 12
![Page 35: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/35.jpg)
ListActivity
sábado, 10 de novembro de 12
![Page 36: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/36.jpg)
public class MainActivity extends ListActivity {
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); List<Cliente> clientes = new ArrayList<Cliente>(); clientes.add(new Cliente("Fulaninha", new Date(1999, 3, 12))); clientes.add(new Cliente("Sicrano", new Date(1980, 4, 2))); clientes.add(new Cliente("Beltrano", new Date(1989, 6, 20))); clientes.add(new Cliente("Fulaninha", new Date(1999, 3, 12))); clientes.add(new Cliente("Sicrano", new Date(1980, 4, 2))); clientes.add(new Cliente("Beltrano", new Date(1989, 6, 20))); clientes.add(new Cliente("Fulaninha", new Date(1999, 3, 12))); clientes.add(new Cliente("Sicrano", new Date(1980, 4, 2))); clientes.add(new Cliente("Beltrano", new Date(1989, 6, 20))); clientes.add(new Cliente("Fulaninha", new Date(1999, 3, 12))); clientes.add(new Cliente("Sicrano", new Date(1980, 4, 2))); clientes.add(new Cliente("Beltrano", new Date(1989, 6, 20))); setListAdapter(new ClienteAdapter(this, clientes)); }
}
sábado, 10 de novembro de 12
![Page 37: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/37.jpg)
sábado, 10 de novembro de 12
![Page 38: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/38.jpg)
Data feia :(
sábado, 10 de novembro de 12
![Page 39: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/39.jpg)
Vamos deixa-la bonita, então :)
sábado, 10 de novembro de 12
![Page 40: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/40.jpg)
Vamos lá no nosso Adapter
sábado, 10 de novembro de 12
![Page 41: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/41.jpg)
Vamos mexer aqui
holder.txtDataNascimento.setText(cliente.getDataNascimento().toGMTString());
sábado, 10 de novembro de 12
![Page 42: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/42.jpg)
Ficando assim:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");String dataFormatada = simpleDateFormat.format(cliente.getDataNascimento());holder.txtDataNascimento.setText(dataFormatada);
sábado, 10 de novembro de 12
![Page 43: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/43.jpg)
sábado, 10 de novembro de 12
![Page 44: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/44.jpg)
Exercício
• Criar 1 classe
• Fazer tela de lista para esta classe criada
sábado, 10 de novembro de 12
![Page 45: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/45.jpg)
Relembrando...
sábado, 10 de novembro de 12
![Page 46: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/46.jpg)
DatabaseHelper
sábado, 10 de novembro de 12
![Page 47: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/47.jpg)
Ajudante
sábado, 10 de novembro de 12
![Page 48: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/48.jpg)
DAOData Access Object
sábado, 10 de novembro de 12
![Page 49: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/49.jpg)
Persistindo um objeto
dao.create(objeto);
sábado, 10 de novembro de 12
![Page 50: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/50.jpg)
Buscando todos os objetos
dao.queryForAll()
sábado, 10 de novembro de 12
![Page 51: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/51.jpg)
Buscando por id
dao.queryForId(id)
sábado, 10 de novembro de 12
![Page 52: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/52.jpg)
Buscando por qualquer campo
dao.queryForEq(fieldName, value)
sábado, 10 de novembro de 12
![Page 53: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/53.jpg)
Relacionamento
sábado, 10 de novembro de 12
![Page 54: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/54.jpg)
Vamos imaginar...
sábado, 10 de novembro de 12
![Page 55: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/55.jpg)
Categoria
Produto
Tem
sábado, 10 de novembro de 12
![Page 56: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/56.jpg)
Vamos precisar de 4 classes
2 Classes modelo2 classes DAO
sábado, 10 de novembro de 12
![Page 57: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/57.jpg)
Modelos e DAOs
sábado, 10 de novembro de 12
![Page 58: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/58.jpg)
Categoria
@DatabaseTablepublic class Categoria { @DatabaseField(generatedId=true) private Integer id; @DatabaseField private String descricao;
sábado, 10 de novembro de 12
![Page 59: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/59.jpg)
Categoria DAO
public class CategoriaDao extends BaseDaoImpl<Categoria, Integer>{
public CategoriaDao(ConnectionSource connectionSource) throws SQLException {
super(connectionSource, Categoria.class); }
sábado, 10 de novembro de 12
![Page 60: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/60.jpg)
Produto
@DatabaseTablepublic class Produto {
@DatabaseField(generatedId=true) private Integer id; @DatabaseField private String nome; @DatabaseField(canBeNull = false, foreign = true) private Categoria categoria;
sábado, 10 de novembro de 12
![Page 61: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/61.jpg)
Produto DAO
public class CategoriaDao extends BaseDaoImpl<Categoria, Integer>{
public CategoriaDao(ConnectionSource connectionSource) throws SQLException {
super(connectionSource, Categoria.class); }
sábado, 10 de novembro de 12
![Page 62: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/62.jpg)
Exemplo
DatabaseHelper helper = new DatabaseHelper(this);try { CategoriaDao categoriaDao = new CategoriaDao(helper.getConnectionSource()); ProdutoDao produtoDao = new ProdutoDao(helper.getConnectionSource());
Categoria categoriaComida = new Categoria("Comida"); categoriaDao.create(categoriaComida); Produto produtoSushi = new Produto("Sushi", categoriaComida); produtoDao.create(produtoSushi); Produto produtoPersistido = produtoDao.queryForAll().get(0); System.out.println("Produto: " + produtoPersistido.getNome()); System.out.println("Categoria: "+produtoPersistido.getCategoria().getDescricao()); System.out.println("CategoriaId: "+produtoPersistido.getCategoria().getId());
sábado, 10 de novembro de 12
![Page 63: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/63.jpg)
Vamos ver a saída
sábado, 10 de novembro de 12
![Page 64: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/64.jpg)
Output
I/System.out(21923): Produto: SushiI/System.out(21923): Categoria: nullI/System.out(21923): CategoriaId: 1
sábado, 10 de novembro de 12
![Page 65: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/65.jpg)
ExemploDatabaseHelper helper = new DatabaseHelper(this);try { CategoriaDao categoriaDao = new CategoriaDao(helper.getConnectionSource()); ProdutoDao produtoDao = new ProdutoDao(helper.getConnectionSource());
Categoria categoriaComida = new Categoria("Comida"); categoriaDao.create(categoriaComida); Produto produtoSushi = new Produto("Sushi", categoriaComida); produtoDao.create(produtoSushi); Produto produtoPersistido = produtoDao.queryForAll().get(0); System.out.println("Produto: " + produtoPersistido.getNome()); System.out.println("Categoria: "+produtoPersistido.getCategoria().getDescricao()); System.out.println("CategoriaId: "+produtoPersistido.getCategoria().getId()); categoriaDao.refresh(produtoPersistido.getCategoria()); System.out.println("Refresh"); System.out.println("Produto: " + produtoPersistido.getNome()); System.out.println("Categoria: "+produtoPersistido.getCategoria().getDescricao()); System.out.println("CategoriaId: "+produtoPersistido.getCategoria().getId());
sábado, 10 de novembro de 12
![Page 66: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/66.jpg)
Vamos ver a saída
sábado, 10 de novembro de 12
![Page 67: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/67.jpg)
Output
I/System.out(21923): Produto: SushiI/System.out(21923): Categoria: nullI/System.out(21923): CategoriaId: 1I/System.out(21923): RefreshI/System.out(21923): Produto: SushiI/System.out(21923): Categoria: Comida
sábado, 10 de novembro de 12
![Page 68: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/68.jpg)
Exercício
Faça um relacionamento.Crie 2 classes models e 2
classes DAOs.Persista os dados e consulte-as
imprimindo no log.
sábado, 10 de novembro de 12
![Page 69: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/69.jpg)
Exercício
Quando finalizar, ao invés de mostrar os dados persistidos
no log, faça duas listas.Uma para cada modelo criado.
sábado, 10 de novembro de 12
![Page 70: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/70.jpg)
Câmera
sábado, 10 de novembro de 12
![Page 71: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/71.jpg)
Intent
android.media.action.IMAGE_CAPTURE
sábado, 10 de novembro de 12
![Page 72: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/72.jpg)
Permissão
<uses-permission android:name="android.permission.CAMERA" />
sábado, 10 de novembro de 12
![Page 73: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/73.jpg)
Exemplo
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");startActivity(intent);
sábado, 10 de novembro de 12
![Page 74: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/74.jpg)
Lembrando...
sábado, 10 de novembro de 12
![Page 75: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/75.jpg)
Exemplo<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" >
<Button android:id="@+id/botao" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Chamar Camera" />
</RelativeLayout>
sábado, 10 de novembro de 12
![Page 76: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/76.jpg)
public class MainActivity extends Activity implements OnClickListener { private Button botao;
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); botao = (Button) findViewById(R.id.botao); botao.setOnClickListener(this);
}
@Override public void onClick(View v) { Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); startActivity(intent); } }
sábado, 10 de novembro de 12
![Page 77: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/77.jpg)
sábado, 10 de novembro de 12
![Page 78: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/78.jpg)
sábado, 10 de novembro de 12
![Page 79: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/79.jpg)
Vamos fazer o seguinte...
sábado, 10 de novembro de 12
![Page 80: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/80.jpg)
Ao tirar foto, vamos exibi-la em um
ImageView
sábado, 10 de novembro de 12
![Page 81: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/81.jpg)
Como?
sábado, 10 de novembro de 12
![Page 82: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/82.jpg)
onActivityResult
startActivityForResult
sábado, 10 de novembro de 12
![Page 83: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/83.jpg)
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >
<Button android:id="@+id/botao" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Chamar Camera" />
<ImageView android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</LinearLayout>
sábado, 10 de novembro de 12
![Page 84: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/84.jpg)
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");startActivityForResult(intent, 19);
sábado, 10 de novembro de 12
![Page 85: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/85.jpg)
Recebe
onActivityResult
sábado, 10 de novembro de 12
![Page 86: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/86.jpg)
protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 19) { Bundle bundle = data.getExtras(); if (bundle != null) { Bitmap bitmap = (Bitmap) bundle.get("data"); image.setImageBitmap(bitmap); } } }
sábado, 10 de novembro de 12
![Page 87: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/87.jpg)
sábado, 10 de novembro de 12
![Page 88: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/88.jpg)
Foi salvo uma foto na galeria de fotos do
android.
sábado, 10 de novembro de 12
![Page 89: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/89.jpg)
É possível definir um local para a imagem ir
sábado, 10 de novembro de 12
![Page 90: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/90.jpg)
Utilizando o putExtra na intent.
sábado, 10 de novembro de 12
![Page 91: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/91.jpg)
Utilizando o putExtra na intent.
Lembra dele?
sábado, 10 de novembro de 12
![Page 92: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/92.jpg)
Através do putExtra podemos
passar o local desejado
sábado, 10 de novembro de 12
![Page 93: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/93.jpg)
Vamos lá!
sábado, 10 de novembro de 12
![Page 94: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/94.jpg)
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");File photo = new File(Environment.getExternalStorageDirectory(), "fotxinha.jpg");intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));imageUri = Uri.fromFile(photo);startActivityForResult(intent, 19);startActivity(intent);
sábado, 10 de novembro de 12
![Page 95: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/95.jpg)
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");File photo = new File(Environment.getExternalStorageDirectory(), "fotxinha.jpg");intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));imageUri = Uri.fromFile(photo);startActivityForResult(intent, 19);startActivity(intent);
sábado, 10 de novembro de 12
![Page 96: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/96.jpg)
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");File photo = new File(Environment.getExternalStorageDirectory(), "fotxinha.jpg");intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));imageUri = Uri.fromFile(photo);startActivityForResult(intent, 19);startActivity(intent);
sábado, 10 de novembro de 12
![Page 97: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/97.jpg)
URI
http://www.faqs.org/rfcs/rfc2396.html
http://developer.android.com/reference/android/net/Uri.html
sábado, 10 de novembro de 12
![Page 98: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/98.jpg)
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 19) { Uri selectedImage = imageUri; getContentResolver().notifyChange(selectedImage, null); ImageView imageView = (ImageView) findViewById(R.id.image); ContentResolver cr = getContentResolver(); Bitmap bitmap; try { bitmap = android.provider.MediaStore.Images.Media .getBitmap(cr, selectedImage);
imageView.setImageBitmap(bitmap); Toast.makeText(this, selectedImage.toString(), Toast.LENGTH_LONG).show(); } catch (Exception e) { Toast.makeText(this, "Falha", Toast.LENGTH_SHORT).show(); Log.e("Camera", e.toString()); }
} }
sábado, 10 de novembro de 12
![Page 99: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/99.jpg)
ExercicioFazer a aplicação tirar uma foto, salvar no lugar
desejado e exibi-la em um imageView
sábado, 10 de novembro de 12
![Page 100: Aula android 04](https://reader034.vdocuments.pub/reader034/viewer/2022042607/554f82fbb4c905d25b8b4b1c/html5/thumbnails/100.jpg)
Próxima aulaJackson
Testes automatizados (Unitários e funcionais)
sábado, 10 de novembro de 12