Listview is catching up

Asked

Viewed 691 times

1

I’ve been ListView

layout.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    xmlns:ads = "http://schemas.android.com/apk/res-auto"
    android:background="#ffffff"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/praias"
        android:layout_centerHorizontal="true"
        android:layout_above="@+id/adView" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/banner_unit_id"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

</RelativeLayout>

Main.class:

adapter = new AdapterListView(MainActivity.this,this,itens,height,width,daoItem,getPackageName());
praias.setAdapter(adapter);
praias.setClickable(true);

Adapter.class:

public View getView(final int position, View v, ViewGroup parent) {
    view = mInflater.inflate(R.layout.layoutlist, null);
    final ListenerItem categoria = itens.get(position);

    TextView nomePraia = (TextView) view.findViewById(R.id.nomePraia);
    ImageView foto = (ImageView) view.findViewById(R.id.fotoPraia);
    final ImageButton btFavorito = (ImageButton) view.findViewById(R.id.btFavorito);
    TextView descricao = (TextView) view.findViewById(R.id.descricao);
    final Button bt = (Button) view.findViewById(R.id.bt);
    Button btVerMais = (Button) view.findViewById(R.id.btVerMais);
    String desc="";
    if(categoria.descricao.length()<397){
        btVerMais.setVisibility(View.INVISIBLE);
        desc=categoria.descricao;
    }else{
        for(int i=0;i<397;i++){
            desc=desc+categoria.descricao.charAt(i);
        }
        desc=desc+"...";
    }
    nomePraia.setText(categoria.nome);
    String nome_foto = categoria.nome_foto;
    int drawableId = context.getResources().getIdentifier(nome_foto, "drawable", packageName);
    Log.e(R.drawable.itanhaem3+"",drawableId+"");
    Bitmap bm = BitmapFactory.decodeResource(context.getResources(), drawableId);
    bm = bm.createScaledBitmap(bm, width, width, true);
    foto.setImageBitmap(bm);

    descricao.setText(desc);

    if (categoria.favorito) {
        btFavorito.setImageResource(R.drawable.ic_action_favorite_v);
    } else {
        btFavorito.setImageResource(R.drawable.ic_action_favorite);
    }
    btVerMais.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ListenerItem listenerItem = new ListenerItem();

            listenerItem = itens.get(position);
            open(listenerItem);
        }
    });
    btFavorito.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ListenerItem listenerItem = new ListenerItem();

            listenerItem = itens.get(position);

            if (listenerItem.favorito) {
                listenerItem.favorito = false;
                btFavorito.setImageResource(R.drawable.ic_action_favorite);
                chamaToast(false);
            } else {
                listenerItem.favorito = true;
                btFavorito.setImageResource(R.drawable.ic_action_favorite_v);
                chamaToast(true);
            }

            try {
                daoItem.createOrUpdate(listenerItem);

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    });
    activity.registerForContextMenu(bt);

    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            bt.setId(position);
            activity.openContextMenu(bt);

        }
    });

    return view;
}

public void open(ListenerItem listenerItem){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
    alertDialogBuilder.setMessage(Html.fromHtml(listenerItem.descricao));
    alertDialogBuilder.setPositiveButton(R.string.voltar,null);
    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
}

Whenever this going from one item to the other hangs a little , I do not know why it is so slow this ListView

Screenshots:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • How so is crashing, you mean at upgrade time?

  • as you go down the scroll , going to the items below, there it is slow, it seems that and time to load the item below

  • Actually the items are already loaded.. has images in this listview?

  • yes, there’s an image. I’ll put a picture

1 answer

1


The problem is that Voce is processing your image within your Thread UI.

To solve this problem, Google itself recommends AsyncTask, that runs everything in the background and outside of your Thread UI.

public class LoadBitmapImage extends AsyncTask<Integer, Void, Bitmap> {
    private final WeakReference<ImageView> imageViewReference;

    public LoadBitmapImage(ImageView imageView) {
        // Use a WeakReference para ter certeza que sua ImageView sera reciclada
        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    // Carregando a imagem em background
    @Override
    protected Bitmap doInBackground(Integer... drawableId) {
        // Aqui voce utiliza seus proprios metodos para carregar e configurar sua Bitmap
        Bitmap bm = BitmapFactory.decodeResource(context.getResources(), drawableId);
        bm = bm.createScaledBitmap(bm, width, width, true);
        return bm;
    }

    // Quando completo, coloque o Bitmap em sua ImageView
    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (imageViewReference != null && bitmap != null) {
            final ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                imageView.setImageBitmap(bitmap);
            }
        }
    }
}

And to upload your image, simply:

...
ImageView foto = (ImageView) view.findViewById(R.id.fotoPraia);
LoadBitmapImage bitmapTask = new LoadBitmapImage(foto);
bitmapTask.execute(drawableId);             
...

Reference: http://developer.android.com/training/displaying-bitmaps/process-bitmap.html

  • If you are passing the listview without stopping to wait to load the image or show any image, can you use it there to load all the images as soon as you enter the page? this method is taking time to load the image and if you pass the item without waiting to load the image it does not appear

  • @Ilgnerdeoliveira this is nothing performatic for your application. Login on a screen and load all drawables in background. In addition to consuming a lot of memory, it can give problems of Outofmemory. It is at your discretion.

  • 'more like this I can’t even use it because it’s taking too long to load the image , I was thinking about uploading a couple of photos in front of the one that the person is in it or something because that way it’s just starting to load when the item arrives and the text loads first and there is the text without the photo

  • @Ilgnerdeoliveira try to implement in a way that suits you! You can create a cache of the images and work with them. But try using this method to load the images so you don’t face memory issues

  • I’ll look here and anything that goes wrong I ask again

Browser other questions tagged

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