Skip to content

Instantly share code, notes, and snippets.

@ajamaica
Created November 24, 2012 22:00
Show Gist options
  • Save ajamaica/4141564 to your computer and use it in GitHub Desktop.
Save ajamaica/4141564 to your computer and use it in GitHub Desktop.
Adaptador para android explicado
/**
* Created with IntelliJ IDEA.
* User: arturojamaicagarcia
* Date: 14/07/12
* Time: 04:17
* To change this template use File | Settings | File Templates.
*/
// Declaramos el Adaptador usamos templates
public class BaseAdapter extends ArrayAdapter<Object> {
Context context;
int layoutResourceId;
ParseObject data[] = null;
//Forzamos a crear un adaptador con el tipo que definimos
public BaseAdapter(Context context, int layoutResourceId, Object[] data) {
//Set de todo localmente
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
//metodo importante para pintar cada Celda
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Creamos la celda
View row = convertView;
PartidoHolder holder = null;
if(row == null)
{
// Le asginamos diseño
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
// Usamos un objeto Holder que solo Almacena las relaciones de interfaz
holder = new Holder();
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
holder.txtTitle2 = (TextView)row.findViewById(R.id.txtTitle2);
}
else
{
holder = (Holder)row.getTag();
}
// Sacamos el objeto del arreglo data y asignamos los valores a cada dato
Object single = data[position];
holder.txtTitle.setText(single.atributo1));
holder.txtTitle2.setText(single.atributo1);
// Regresamos el row ya pintadito
return row;
}
static class Holder
{
TextView txtTitle;
TextView txtTitle2;
}
}
// Asi seteamos el Adaptador
BaseAdapter adapter = new BaseAdapter(this, R.layout.row, data);
listView.setAdapter(adapter);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment