CustomGridAdapter.java


Dosyayı İndir
package com.godoro.androidcustoms;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.BufferedInputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

public class CustomGridAdapter  extends BaseAdapter {

    private Activity activity;

    private List<CustomGridEntity> entityList;

    public CustomGridAdapter(Activity activity, List<CustomGridEntity> entityList) {
        this.activity = activity;
        this.entityList = entityList;
    }
    public int getCount() {
        return entityList.size();
    }

    public Object getItem(int position) {
        return entityList.get(position);
    }

    public long getItemId(int position) {
        return entityList.get(position).getEntityId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view=convertView;
        if(view==null){
            view=activity.getLayoutInflater().inflate(R.layout.custom_grid_cell, null);
        }
        TextView titleView=(TextView)view.findViewById(R.id.titleView);
        final ImageView remoteImage=(ImageView)view.findViewById(R.id.remoteImage);
        final CustomGridEntity entity=entityList.get(position);
        titleView.setText(entity.getEntityTitle());
        Runnable runnable=new Runnable() {
            @Override
            public void run() {
                final Bitmap bitmap=downloadBitmap(entity.getImageAddress());
                if(bitmap==null){
                    return;
                }
                Runnable action=new Runnable() {
                    @Override
                    public void run() {
                        remoteImage.setImageBitmap(bitmap);
                    }
                };
                activity.runOnUiThread(action);
            }
        };
        Thread thread=new Thread(runnable);
        thread.start();

        return view;
    }
    private static Bitmap downloadBitmap(String imageAddress){
        try {
            URL url = new URL(imageAddress);
            URLConnection conn = url.openConnection();
            BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
            Bitmap bitmap = BitmapFactory.decodeStream(bis);
            return bitmap;
        }catch (Exception e){
            Log.e("GodoroAndroid","Görüntü indirme yanlışlığı",e);
            return null;

        }
    }
}

				
Dosyayı İndir

Bu Sayfayı Paylaş:




Bu Sayfayı Paylaş:

İletişim Bilgileri

Takip Et

Her Hakkı Saklıdır. Bu sitede yayınlanan tüm bilgi ve fikirlerin kullanımından fibiler.com sorumlu değildir. Bu sitede üretilmiş , derlenmiş içerikleri, fibiler.com'u kaynak göstermek koşuluyla kendi sitenizde kullanılabilirsiniz. Ancak telif hakkı olan içeriklerin hakları sahiplerine aittir