来自异步任务的android适配器

时间:2014-04-01 13:23:58

标签: android android-asynctask adapter

在我的某个应用中,我需要使用AsyncTask.我需要从HTTP网站获取图片网址。我已经在doInBackground方法中完成了这项工作。我将该图像的URL作为字符串。

publishProgress(thumb);//thumb is string 

然后在

        @Override
        protected void onProgressUpdate(String... values) {

            super.onProgressUpdate(values);
            sample = new ArrayList<String>();

            System.out.println("this is on progress..."+values[0]);
            sample.add(values[0]);

            GridviewAdapter_old go=new GridviewAdapter_old(getActivity(), sample);

            gv.setAdapter(go);
            //          gv.setAdapter(ga);
        }

public class GridviewAdapter_old extends BaseAdapter

{

    private ArrayList<String> listCountry;

    private Activity activity;

    public GridviewAdapter_old(Activity activity, ArrayList<String> listCountry) {
        super();

        // this.listCountry = listCountry;
        this.listCountry = new ArrayList<String>();
        this.listCountry = listCountry;
        // this.listFlag = listFlag;
        this.activity = activity;
        System.out.println("this is contry name " + this.listCountry);
        // System.out.println("this is img  name  " + this.listFlag);
        // System.out.println();

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        System.out.println("len " + listCountry.size());
        // return listCountry.size();
        return listCountry.size();
    }

    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return listCountry.get(position);
    }

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

    public static class ViewHolder {
        public ImageView imgViewFlag;
        // public TextView txtViewTitle;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // TODO Auto-generated method stub
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();

        if (convertView == null) {
            view = new ViewHolder();
            convertView = inflator.inflate(R.layout.test, null);

            view.imgViewFlag = (ImageView) convertView
                    .findViewById(R.id.grid_item_image);

            // view.imgViewFlag.setBackgroundResource(R.drawable.view_default);
            convertView.setTag(view);

        }

        else {

            view = (ViewHolder) convertView.getTag();
        }
        try {
            URL myUrl = new URL(listCountry.get(0));
            InputStream inputStream = (InputStream) myUrl.getContent();
            Drawable drawable = Drawable.createFromStream(inputStream, null);
            view.imgViewFlag.setImageDrawable(drawable);
        } catch (Exception e) {
            System.out.println(e.getMessage());

        }
        /*
         * Bitmap imageBitmap = null; //System.gc(); try { URL imageURL = new
         * URL(listCountry.get(0));
         * System.out.println("this is in last portion..."
         * +listCountry.get(position)); imageBitmap =
         * BitmapFactory.decodeStream(imageURL.openStream());
         * view.imgViewFlag.setImageBitmap(imageBitmap); //
         * view.txtViewTitle.setText(listCountry.get(position)); //
         * view.imgViewFlag.setImageResource(listFlag.get(position));
         * 
         * } catch (Exception e) { System.out.println("this is error " +
         * e.getMessage()); }
         */

        return convertView;
    }

}

问题是:

  • 我没有从网址获取图片(来自拇指(publishProgress(thumb);
  • 我没有得到多张图片。

1 个答案:

答案 0 :(得分:1)

您只在列表中找到一个,因为每次拨打onProgressUpdate时都会创建一个新列表。

你不想使用onProgressUpdate来填充你的列表,这将是一个疯狂的开销。相反,您想在doInBackground中创建列表,然后将该列表传递给onPostExecute,然后将新列表放到适配器上并调用适配器上的notifyDatasetChanged来刷新列表。

简而言之,创建适配器(类范围变量),创建一个ArrayList(类范围变量),每次进行更改时,您只需要在适配器上调用notifyDatasetCHanged

ArrayList<String> list = new ArrayList<String();
ArrayAdapter adapter;

public class AsyncTask.....

@Override
protected Void doInBackground(Void... params){
    ...
    list.add(stuff);
}

@Override
protected Void onPostExecute(Void params){
    adapter.notifyDatasetChanged();
}