从android中的asynctask启动新线程

时间:2012-04-16 07:06:30

标签: android

在我的应用程序中,我进行了服务调用以从服务器获取图像。为此我使用了asynk任务类。对于图像加载我正在使用延迟加载。现在的问题是,当我打电话从后台方法加载图像asynk任务然后屏幕变得空白一段时间然后我看到进度条。当我为它做跟踪时,我发现我应该在新线程中开始图像加载。所以我想知道如何开始一个新线程在asynk任务背景方法中。请帮助我......我是新手.Below是我的代码..

public class GetRestaurentDetails extends AsyncTask<Object, Object, Object>
{

    Context context;
    ProgressDialog progressDialog;


    public GetRestaurentDetails(Context c) {
        this.context=c;

    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();


        this.progressDialog=ProgressDialog.show(context,"","Please wait...");

    }
    @Override
    protected Object doInBackground(Object... params) {

        onGetRestaurentDetails();
        listAdapter = new RestaurantLidtAdapter(arrImages);
        setListAdapter(listAdapter);

        return null;
    }


    @Override
    protected void onPostExecute(Object result) {
        super.onPostExecute(result);


        progressDialog.dismiss();

    }

}

和适配器类

private class RestaurantLidtAdapter extends BaseAdapter{
    Holder holder = null;
    LayoutInflater inflater;
    ImageLoader imageLoader;
    public RestaurantLidtAdapter(ArrayList<String> arrImages){

        inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(RestaurantListActivity.this);
    }

    @Override
    public int getCount() {

        return arrPromotions.size();
    }

    @Override
    public Object getItem(int position) {

        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub


        if(convertView == null){

            holder = new Holder();
            convertView = (View)inflater.inflate(R.layout.restaurantlistrowlayout, parent, false);
            holder.imgArrowImage = (ImageView)convertView.findViewById(R.id.imgArrowInHotelList);
            holder.imgHotelImage = (ImageView)convertView.findViewById(R.id.rowHotelImage);
            holder.txtRestaurentPromotion = (TextView)convertView.findViewById(R.id.TextViewPromotions);
            holder.txtRestaurentName = (TextView)convertView.findViewById(R.id.TextViewRestaurentName);

            convertView.setTag(holder);
        }
        else
        {
            holder = (Holder)convertView.getTag();


        }

        //holder.imgHotelImage.setImageBitmap(loadBitmap(arrImages.get(position)));


        imageLoader.DisplayImage(arrImages.get(position), holder.imgHotelImage);
        holder.txtRestaurentPromotion.setText(arrPromotions.get(position));
        holder.txtRestaurentName.setText(arrRestaurentName.get(position));
        convertView.setTag(holder);
        return convertView;
    }



}

1 个答案:

答案 0 :(得分:0)

由于您已经在使用Lazy loading listview代码中的ImageLoader类,我认为您不需要实现AsyncTask,因为您只是创建适配器并将其设置为doInBackground()方法内的ListView。

还有一个问题显而易见,因为你试图在后台进程中创建适配器,这是不可取的,你应该在除了doInBackground()之外的onPostExecute()中只设置一次。

现在,我建议的解决方案是从AsyncTask中删除这两行,并将Images URL直接传递给适配器类。