Listview of Android slow

Asked

Viewed 56 times

0

What may 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;
    }
}
  • Testing the app where? right on mobile or desktop?

  • I tested in Mobile and Emulator

1 answer

1


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

@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, like using the Viewholder pattern. But in this case it should be this.

  • public View getView(int i, View convertView, Viewgroup Parent) { final View vista = convertView; if(vista==null) vista = Inflater.inflate(R.layout.element_lista, null);

  • An error occurs "Cannot assing a value to final variable 'view' "

  • Take off the final, or exchange the last two lines you quoted for final View vista = (convertView != null ? convertView : inflater.inflate(R.layout.elemento_lista, null));

  • It compiled however when I open the app to Activity stops working, it happens when I take the end and when I change the lines

  • Logcat java.lang.Nullpointerexception: Attempt to invoke virtual method 'android.view.View android.view.Layoutinflater.inflate(int, android.view.Viewgroup)' on a null Object Reference

  • put the code exactly as I put it now. Take the end and leave it as it is. Formerly it worked?

  • check the line, I changed to Inflater.inflate(R.layout.element_list, Parent, false);

  • It worked when I had the first code I posted, but it kept giving many lags

  • The app compiles however when you open the app’s listview Activity to

  • changed what I changed there? go debugging and see which line gives error.

  • that line is actually after the if? or put inside the if keys to test view = Inflater.inflate(R.layout.element_list, Parent, false);

  • The error line is E/Androidruntime: FATAL EXCEPTION: main

  • but debugging line by line, which gives error?

  • I managed to open the Activity but still gives lags

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.