¿Cómo puedo hacer que mi ArrayAdapter siga el patrón ViewHolder?

Aquí está mi ArrayAdapter. Me gustaría hacer esto más eficiente siguiendo el patrón de ViewHolder:

Http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

Pero no estoy seguro de cómo lograr esto.

UPDATE: Patrón de ViewHolder

private class QuoteAdapter extends ArrayAdapter<Quote> { private ArrayList<Quote> items; // used to keep selected position in ListView private int selectedPos = -1; // init value for not-selected public QuoteAdapter(Context context, int textViewResourceId, ArrayList<Quote> items) { super(context, textViewResourceId, items); this.items = items; } public void setSelectedPosition(int pos) { selectedPos = pos; // inform the view of this change notifyDataSetChanged(); } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; ViewHolder holder; // to reference the child views for later actions if (v == null) { LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.mainrow, null); // cache view fields into the holder holder = new ViewHolder(); holder.nameText = (TextView) v.findViewById(R.id.nameText); holder.priceText = (TextView) v.findViewById(R.id.priceText); holder.changeText = (TextView) v.findViewById(R.id.changeText); // associate the holder with the view for later lookup v.setTag(holder); } else { // view already exists, get the holder instance from the view holder = (ViewHolder)v.getTag(); } // change the row color based on selected state if (selectedPos == position) { v.setBackgroundResource(R.drawable.stocks_selected_gradient); holder.nameText.setTextColor(Color.WHITE); holder.priceText.setTextColor(Color.WHITE); holder.changeText.setTextColor(Color.WHITE); } else { v.setBackgroundResource(R.drawable.stocks_gradient); holder.nameText.setTextAppearance(getApplicationContext(), R.style.BlueText); holder.priceText.setTextAppearance(getApplicationContext(), R.style.BlueText); holder.changeText.setTextAppearance(getApplicationContext(), R.style.BlueText); } Quote q = items.get(position); if (q != null) { if (holder.nameText != null) { holder.nameText.setText(q.getSymbol()); } if (holder.priceText != null) { holder.priceText.setText(q.getLastTradePriceOnly()); } if (holder.changeText != null) { try { float floatedChange = Float.valueOf(q.getChange()); if (floatedChange < 0) { if (selectedPos != position) holder.changeText.setTextAppearance(getApplicationContext(), R.style.RedText); // red } else { if (selectedPos != position) holder.changeText.setTextAppearance(getApplicationContext(), R.style.GreenText); // green } } catch (NumberFormatException e) { System.out.println("not a number"); } catch (NullPointerException e) { System.out.println("null number"); } holder.changeText.setText(q.getChange() + " (" + q.getPercentChange() + ")"); } } return v; } } 

El ViewHolder es básicamente una instancia de clase estática que se asocia con una vista cuando se crea, almacenando en caché las vistas secundarias que está buscando en tiempo de ejecución. Si la vista ya existe, recuperar la instancia de titular y utilizar sus campos en lugar de llamar a findViewById .

En tu caso:

 @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; ViewHolder holder; // to reference the child views for later actions if (v == null) { LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.mainrow, null); // cache view fields into the holder holder = new ViewHolder(); holder.nameText = (TextView) v.findViewById(R.id.nameText); holder.priceText = (TextView) v.findViewById(R.id.priceText); holder.changeText = (TextView) v.findViewById(R.id.changeText); // associate the holder with the view for later lookup v.setTag(holder); } else { // view already exists, get the holder instance from the view holder = (ViewHolder) v.getTag(); } // no local variables with findViewById here // use holder.nameText where you were // using the local variable nameText before return v; } // somewhere else in your class definition static class ViewHolder { TextView nameText; TextView priceText; TextView changeText; } 

Advertencia: No intenté compilar esto, así que tome con un grano de sal.

  • Agregue varios textViews (de anchos variables) en relativeLayout uno tras otro
  • ¿Cómo inhabilito el overscroll y el rebote en una lista de android?
  • Cómo crear ListView con la ayuda de remoteview?
  • Excepciones mientras utiliza MvxAdapter
  • Cómo quitar el borde en un ListView?
  • Cómo llamar a notifyDataSetChanged () desde un adaptador genérico
  • Android: ListView no muestra ningún resultado
  • CheckBox se desactiva en desplazamiento en una vista de lista personalizada
  • Cómo poner editable editable dentro de un listview?
  • Android Cómo reciclar el mapa de bits correctamente cuando se utiliza RecyclerView?
  • Android: Utilizar SimpleCursorAdapter para obtener datos de la base de datos a ListView
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.