0

I have a simple ListView which has image on the left and text in the right. Currently i am using a LazyAdapter to load images from drawable resource according to name of dishes.

I have created a array of image ID's and would be using that to setImageView.

The problem is, the scrolling is not smooth which i know due to the fact that all the image loading is done by the main thread. I googled a lot about AsyncTask, which allows us run background thread. I am not getting how to use AsyncTask to achieve by task as all the examples that i have seen on AsyncTask has image downloaded from URL.

Please help me in using AsyncTask to load list of images from drawable to Bitmap.

Here is my mail class:

ARRAY.image is an integer Array which has image ID's which is used in adapter to set ImageView

package love.cookbook.FirstPage;

import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.MenuItem;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.*;
import android.view.*;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.*;

public class ListViewSampleActivity extends SherlockListActivity {

    LazyAdapter adapter;

    public String packageName;
    public int imageID;
    String ingredientsImageName;

    public MySqliteHelper dbHelper;
    Cursor cur;


    String eachIngredientsImageName [];

    String listItemName;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();

        super.onCreate(savedInstanceState);

        setContentView(R.layout.list_main);
    dbHelper = new MySqliteHelper(this);

    ListView list=(ListView)findViewById(android.R.id.list);

    final FirstPageActivity firstPage = new FirstPageActivity();


        ARRAY.dishes = intent.getStringArrayExtra("DISHES");
        ARRAY.description = intent.getStringArrayExtra("DESCRIPTION");
        ARRAY.timeToPrepare = intent.getStringArrayExtra("TIMETOPREPARE");
        ARRAY.lock = intent.getStringArrayExtra("LOCKVALUE");
        ARRAY.imageName = intent.getStringArrayExtra("IMAGENAME");

        ARRAY.nonVeg = intent.getStringArrayExtra("NONVEG");
        ARRAY.isFavourite = intent.getStringArrayExtra("ISFAVOURITE");

        /*
         * This code snippet is used to fetch the image id from the resource folder from the name fetched from database.
         */
        ARRAY.image = new int[ARRAY.imageName.length];

        packageName=this.getPackageName();
        for(int i=0;i<ARRAY.imageName.length;i++){
            ARRAY.image[i]=getResources().getIdentifier(ARRAY.imageName[i].toLowerCase(), "drawable", packageName);

        }


        adapter=new LazyAdapter (this, ARRAY.dishes,ARRAY.description,ARRAY.timeToPrepare,ARRAY.image,ARRAY.lock,ARRAY.nonVeg);
        list.setAdapter(adapter);

        list.setOnItemClickListener(new OnItemClickListener(){


            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {

            }

        });
    }

}

Here is my Adapter class:

package love.cookbook.FirstPage;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

    public class LazyAdapter extends BaseAdapter {

        private Activity activity;
        private String[] dishes;
        private String[] recipeDescripion;
        private String[] timeToPrepare;
        private String[] lock;
        private String[] nonVeg;
        private int [] images;
        ViewHolder holder;
        Bitmap bitmap;
        private static LayoutInflater inflater=null; 

        public LazyAdapter(Activity a, String[] dishes, String[] recipeDescription,String[] timeToPrepare,int[] images,String [] lock,String [] nonVeg) {
            activity = a;
            this.dishes=dishes;
            this.images=images;
            this.bitmap = bitmap;
            this.recipeDescripion=recipeDescription;
            this.timeToPrepare=timeToPrepare;
            this.lock=lock;
            this.nonVeg=nonVeg;
            inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            //View vi=convertView;
            if(convertView==null){
                //vi = inflater.inflate(R.layout.item, null);
                convertView = inflater.inflate(R.layout.item,parent, false);
                //image = (ImageView)vi.findViewById(R.id.imageView);
                holder = new ViewHolder();
                holder.image = (ImageView) convertView.findViewById(R.id.imageView2);
                holder.vegNonveggImageView = (ImageView) convertView.findViewById(R.id.imageView3);

                holder.textView1 = (TextView)convertView.findViewById(R.id.textView1);
                holder.textView2 = (TextView)convertView.findViewById(R.id.textView2);
                holder.textView3 = (TextView)convertView.findViewById(R.id.textView3);
                convertView.setTag(holder);

            }
            else
                holder = (ViewHolder)convertView.getTag();


            holder.textView1.setText(dishes[position]);

            holder.image.setImageResource(images[position]);

            holder.textView2.setText(recipeDescripion[position]);
            holder.textView3.setText(timeToPrepare[position]); 

            if(nonVeg[position].equals("1")){
                holder.vegNonveggImageView.setImageResource(R.drawable.non_veg_symbol);
            }
            else{
                holder.vegNonveggImageView.setImageResource(R.drawable.veg_symbol);
            }


            return convertView;
        }

        public int getCount() {

            return dishes.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public class ViewHolder{
            ImageView image;
            TextView textView1;
            TextView textView2;
            TextView textView3;
            ImageView vegNonveggImageView;
        }


}
4

1 に答える 1