Android listview with slowness

0

What might be causing slowness?

Code of Adaptador :

public class Adaptador extends BaseAdapter{
    private static LayoutInflater inflater = null;

    Context contexto;
    String[][] datos;
    int[] datosImg;

    public Adaptador(Context conexto, String[][] datos, int[] imagenes)
    {
        this.contexto = conexto;
        this.datos = datos;
        this.datosImg = imagenes;

        inflater = (LayoutInflater)conexto.getSystemService(conexto.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public View getView(int i, View convertView, ViewGroup parent) {
        final View vista = inflater.inflate(R.layout.elemento_lista, null);

        TextView titulo = (TextView) vista.findViewById(R.id.tvTitulo);
        TextView duracion = (TextView) vista.findViewById(R.id.tvDuracion);
        TextView director = (TextView) vista.findViewById(R.id.tvDirector);

        ImageView imagen = (ImageView) vista.findViewById(R.id.ivImagen);
        RatingBar calificacion = (RatingBar) vista.findViewById(R.id.ratingBarPel);

        titulo.setText(datos[i][0]);
        director.setText(datos[i][1]);
        duracion.setText("Duração " + datos[i][2]);
        imagen.setImageResource(datosImg[i]);
        calificacion.setProgress(Integer.valueOf(datos[i][3]));

        return vista;
    }



    @Override
    public int getCount() {
        return datosImg.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
}
    
asked by anonymous 24.10.2018 / 21:31

1 answer

1

Your code is not reusing View and is always inflating. The method runs several times and always inflates. You can take advantage of convertView. In this case, do this:

@Override
public View getView(int i, View convertView, ViewGroup parent) {
     View vista = convertView;

    if(vista==null)
        vista = inflater.inflate(R.layout.elemento_lista, parent, false);

    TextView titulo = (TextView) vista.findViewById(R.id.tvTitulo);
    TextView duracion = (TextView) vista.findViewById(R.id.tvDuracion);
    TextView director = (TextView) vista.findViewById(R.id.tvDirector);

    ImageView imagen = (ImageView) vista.findViewById(R.id.ivImagen);
    RatingBar calificacion = (RatingBar) vista.findViewById(R.id.ratingBarPel);

    titulo.setText(datos[i][0]);
    director.setText(datos[i][1]);
    duracion.setText("Duração " + datos[i][2]);
    imagen.setImageResource(datosImg[i]);
    calificacion.setProgress(Integer.valueOf(datos[i][3]));

    return vista;
}

Of course there are other ways to improve, such as using the ViewHolder standard. But in that case it should be that.

    
24.10.2018 / 22:16