Permalink
Browse files

Extracao do adapter da lista de eventos

  • Loading branch information...
1 parent 98e75d4 commit f908ef52012d5068c33878511426f5dcb0ab927a @danielpassos danielpassos committed Jan 21, 2011
View
@@ -1,14 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
- android:id="@+id/linha"
+ android:id="@+item_lista_evento/linha"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
- <ImageView android:id="@+id/logo" android:layout_width="100px" android:layout_height="100px" />
+ <ImageView android:id="@+item_lista_evento/logo" android:layout_width="100px" android:layout_height="100px" />
- <TextView android:id="@+id/texto"
+ <TextView android:id="@+item_lista_evento/texto"
android:paddingLeft="10px"
android:paddingTop="10px"
android:textColor="#444444"
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+ <color name="backgroud_branco">#ffe6f0ff</color>
+ <color name="background_cinza">#A9A9A9</color>
+</resources>
@@ -1,37 +1,24 @@
package br.com.agendatech.cadastro;
-import java.io.IOException;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import android.app.ListActivity;
import android.app.ProgressDialog;
-import android.content.Context;
import android.content.Intent;
-import android.graphics.Bitmap;
-import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
-import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
-import android.view.ViewGroup;
import android.widget.AdapterView;
-import android.widget.AdapterView.OnItemLongClickListener;
-import android.widget.ArrayAdapter;
-import android.widget.ImageView;
-import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
-import br.com.agendatech.cadastro.R;
+import android.widget.AdapterView.OnItemLongClickListener;
import br.com.agendatech.modelo.Evento;
import br.com.agendatech.servico.EventoParser;
@@ -107,53 +94,13 @@ protected void onPreExecute() {
@Override
protected void onPostExecute(List<Evento> eventos) {
- LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
-
- final List<View> views = new ArrayList<View>();
- for (int i = 0; i < eventos.size() ; i++) {
- View view = li.inflate(R.layout.item, null);
- LinearLayout ll = (LinearLayout)view.findViewById(R.id.linha);
-
- Evento evento = eventos.get(i);
-
- if(i%2 == 0){
- ll.setBackgroundColor(0xFFf9f9f9);
-
- }else{
- ll.setBackgroundColor(0xFFe6e6e6);
- }
-
-
-
- TextView texto = (TextView) view.findViewById(R.id.texto);
- texto.setText(evento.getNome() + "\n" + evento.getData());
- ImageView iv = (ImageView) view.findViewById(R.id.logo);
- try {
- Bitmap b = BitmapFactory.decodeStream(new URL(evento.getLogo()).openStream());
- iv.setImageBitmap(b);
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- views.add(view);
- }
-
-
- setListAdapter(new ArrayAdapter<Evento>(ListaEventos.this, android.R.layout.simple_list_item_1, eventos){
- @Override
- public View getView(int position, View convertView,
- ViewGroup parent) {
- return views.get(position);
- }
- });
+ setListAdapter(new ListaEventosAdapter(ListaEventos.this, R.layout.item, eventos));
progress.dismiss();
if(!retrive) {
TextView tv = (TextView) findViewById(android.R.id.empty);
tv.setText(getString(R.string.erro_buscar_evento));
}
}
}
+
}
@@ -0,0 +1,88 @@
+package br.com.agendatech.cadastro;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+import android.content.Context;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.ImageView;
+import android.widget.LinearLayout;
+import android.widget.TextView;
+import br.com.agendatech.modelo.Evento;
+
+public class ListaEventosAdapter extends ArrayAdapter<Evento> {
+
+ private List<Evento> eventos;
+ private Context context;
+ private int resource;
+ private List<View> views;
+
+ public ListaEventosAdapter(Context context, int resource, List<Evento> eventos) {
+ super(context, resource, eventos);
+ this.eventos = eventos;
+ this.context = context;
+ this.resource = resource;
+ // Pre-load list to get event image
+ this.views = montaViews();
+ }
+
+ @Override
+ public Evento getItem(int position) {
+ return eventos.get(position);
+ }
+
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent) {
+ return views.get(position);
+ }
+
+ private List<View> montaViews() {
+
+ LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+
+ List<View> views = new ArrayList<View>();
+ for (int i = 0; i < eventos.size() ; i++) {
+
+ View view = layoutInflater.inflate(resource, null);
+
+ Evento evento = eventos.get(i);
+
+ LinearLayout ll = (LinearLayout) view.findViewById(R.item_lista_evento.linha);
+
+ if(i % 2 == 0) {
+ ll.setBackgroundColor(context.getResources().getColor(R.color.backgroud_branco));
+ } else{
+ ll.setBackgroundColor(context.getResources().getColor(R.color.background_cinza));
+ }
+
+ TextView texto = (TextView) view.findViewById(R.item_lista_evento.texto);
+ texto.setText(evento.getNome() + "\n" + evento.getData());
+
+ ImageView imageEvento = (ImageView) view.findViewById(R.item_lista_evento.logo);
+ try {
+ Bitmap b = BitmapFactory.decodeStream(new URL(evento.getLogo()).openStream());
+ imageEvento.setImageBitmap(b);
+ } catch (MalformedURLException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ views.add(view);
+
+ }
+
+ return views;
+ }
+
+}

0 comments on commit f908ef5

Please sign in to comment.