¿Cómo implementar un titular de vista?

Estoy utilizando un espectador para mostrar desde un arrayadapter.it dinámico funciona, pero los datos que se muestran cambian de forma irregular cuando desplazo la lista. Quiero que mi vista de lista se llene sólo una vez, no todo el tiempo cuando desplazo mi lista. ¿Cualquier sugerencia? Aquí está mi código

public View getView(int position, View convertView, ViewGroup parent) { // A ViewHolder keeps references to children views to avoid unneccessary calls // to findViewById() on each row. ViewHolder holder; // When convertView is not null, we can reuse it directly, there is no need // to reinflate it. We only inflate a new View when the convertView supplied // by ListView is null. if (convertView == null) { convertView = mInflater.inflate(R.layout.sample, null); // Creates a ViewHolder and store references to the two children views // we want to bind data to. holder = new ViewHolder(); holder.name = (TextView) convertView.findViewById(R.id.text); holder.icon = (ImageView) convertView.findViewById(R.id.icon); convertView.setTag(holder); } else { // Get the ViewHolder back to get fast access to the TextView // and the ImageView. holder = (ViewHolder) convertView.getTag(); } // Bind the data efficiently with the holder. if(_first==true) { if(id<myElements.size()) { holder.name.setText(myElements.get(id)); holder.icon.setImageBitmap( mIcon1 ); id++; } else { _first=false; } } //holder.icon.setImageBitmap(mIcon2); /*try{ if(id<myElements.size()) id++; else { id--; } } catch(Exception e) { android.util.Log.i("callRestService",e.getMessage()); }*/ return convertView; } static class ViewHolder { TextView name; ImageView icon; } 

Cuando se carga la lista se ve así: http://i.stack.imgur.com/NrGhR.png texto alternativo Después de desplazar algunos datos http://i.stack.imgur.com/sMbAD.png texto alternativo Parece que esto, y de nuevo si me desplácese hasta el principio se ve http://i.stack.imgur.com/0KjMa.png texto alternativo

PS: mi lista tiene que estar en orden alfabético

Usted tendría que escribir su propia versión de ListView para hacer eso (que es malo). Si el ListView no funciona correctamente, probablemente significa que está haciendo algo malo.

¿De dónde proviene el elemento id ? Usted está obteniendo la posición en su método getView() , por lo que no necesita preocuparse por exceder los límites de la lista. La posición está vinculada a la posición del elemento en su lista, por lo que puede obtener el elemento correcto como este:

 myElements.get(position); 

Cuando cambia los datos de su lista, puede llamar a esto:

 yourAdapter.notifyDataSetChanged() 

Que va a reconstruir su lista con nuevos datos (mientras mantiene su desplazamiento y cosas).

¿Has probado esto?

 public View getView(int position, View convertView, ViewGroup parent) { // A ViewHolder keeps references to children views to avoid unneccessary calls // to findViewById() on each row. ViewHolder holder; // When convertView is not null, we can reuse it directly, there is no need // to reinflate it. We only inflate a new View when the convertView supplied // by ListView is null. if (convertView == null) { convertView = mInflater.inflate(R.layout.sample, null); // Creates a ViewHolder and store references to the two children views // we want to bind data to. holder = new ViewHolder(); holder.name = (TextView) convertView.findViewById(R.id.text); holder.icon = (ImageView) convertView.findViewById(R.id.icon); convertView.setTag(holder); } else { // Get the ViewHolder back to get fast access to the TextView // and the ImageView. holder = (ViewHolder) convertView.getTag(); } // Bind the data efficiently with the holder. holder.name.setText(myElements.get(id)); holder.icon.setImageBitmap( mIcon1 ); return convertView; } static class ViewHolder { TextView name; ImageView icon; } 

Si es así, ¿qué tiene de malo?

No creo que cargar todas las filas a la vez es una buena idea. Usted terminará para arriba tener un montón de vistas inútiles en la memoria que van a retardar la aplicación abajo para nada. Las vistas y las operaciones en las vistas (como inflar, findViewById, getChild ..) son costosas, usted debe intentar reutilizarlas tanto como sea posible. Es por eso que utilizamos ViewHolders.

  • Android obtiene la vista de Preferencia en PreferenciaActividad
  • Android: Layout.addView () no funciona
  • ¿Puedo escalar una vista en Android?
  • Vista de la actividad actual de Fade Out
  • View getWidth () y getHeight () devuelven 0
  • Error al inflar la vista de clase interna
  • Android findViewById () en la vista personalizada
  • ¿Cómo hacer una devolución de llamada después de que la vista esté completamente renderizada?
  • Deslizar a otra vista en recyclerVer con ItemTouchHelper
  • Página Curl Animation en android?
  • View.onMeasure () no se llama
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.