ListView is crashing

1

I have 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 you are moving from one item to another, it will hang a little, I do not know why this is so slow ListView

Screenshots:

    
asked by anonymous 27.03.2015 / 19:32

1 answer

1

The problem is that you are processing your image inside your UI Thread.

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

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, you simply:

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

Reference: link

    
31.03.2015 / 19:03