使用Universal Image Loader的Listview

时间:2014-05-20 13:08:58

标签: android android-listview android-asynctask android-image universal-image-loader

使用Universal Image Loader加载我的listview时遇到问题。问题是我不知道函数调用它的位置因为总是出现RuntimeException。异常是由于doinBackground中的ImageLoader。

我的代码如下:

公共类Listview extends Activity实现了View.OnClickListener {

private ListView lstView;
private ImageAdapter imageAdapter;

ArrayList<HashMap<String, Object>> MyArrList = new ArrayList<HashMap<String, Object>>();

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    // ProgressBar
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

    setContentView(R.layout.activity_list);

    // ListView and imageAdapter
    lstView = (ListView) findViewById(R.id.listView1);
    lstView.setClipToPadding(false);
    imageAdapter = new ImageAdapter(getApplicationContext());
    imageAdapter.notifyDataSetChanged();//When add new photos, refresh the view
    lstView.setAdapter(imageAdapter);

    lstView.invalidateViews();

    setProgressBarIndeterminateVisibility(true);
    new LoadContentFromServer().execute();

}


class LoadContentFromServer extends AsyncTask<Object, Integer, Object> {

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

        String url = "http://xxxxxxxxxxxxxx/xxxxxxx/Xxxx.php";
        String photo_id_new;
        String photo_id_prev = "";
        JSONArray data;
        imageAdapter.notifyDataSetChanged();

        try {
            data = new JSONArray(getJSONUrl(url));

            MyArrList = new ArrayList<HashMap<String, Object>>();
            HashMap<String, Object> map;

            for(int i = 0; i < data.length(); i++) {

                JSONObject c = data.getJSONObject(i);
                map = new HashMap<String, Object>();

                photo_id_new = c.getString("photo_id");

                map.put("ImageID", c.getString("photo_id"));

                map.put("NameAcc", c.getString("nameacc"));
                map.put("PriceAcc", c.getString("price"));
                map.put("BrandAcc", c.getString("brand"));
                map.put("Shopname", c.getString("shopname"));
                map.put("Shopcity", c.getString("city"));
                map.put("Shopcountry", c.getString("country"));

                map.put("UserName", c.getString("forename"));
                map.put("UserSurname", c.getString("surname"));

                //If the new photo it's different to the previous, doesn't show and create more rows
                if(!photo_id_prev.equals(photo_id_new)){
                    // Thumbnail Get ImageBitmap To Object
                    //Bitmap newBitmap = loadBitmap(c.getString("hyperlink"));
                    Bitmap newBitmap2 = ImageLoader.getInstance().loadImageSync(c.getString("hyperlink"));
                    Log.i("INFO", "Hyperlink:  " + c.getString("hyperlink"));

                    map.put("ImageThumBitmap", newBitmap2);
                    map.put("Repeat", "1"); //To know if it is a new photo

                } else {
                    // no new photo
                    map.put("Repeat", "0");
                    Log.i("INFO", "Inside else");

                }

                photo_id_prev = photo_id_new;
                Log.i("INFO", "Previous photo id:  "+photo_id_prev);

                MyArrList.add(map);

                publishProgress(i);

            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e){
            e.printStackTrace();

        }

        return null;
    }

    @Override
    public void onProgressUpdate(Integer... progress) {
        imageAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onPostExecute(Object result) {
        setProgressBarIndeterminateVisibility(false); // When Finish
    }
}

public class ImageAdapter extends BaseAdapter {

    private Context mContext;

    public ImageAdapter(Context context) {
        mContext = context;
    }

    public int getCount() {
        return MyArrList.size();
    }


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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        //View row = convertView;
        ViewHolder holder;

        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .resetViewBeforeLoading(true)
                .cacheInMemory(true)
                .cacheOnDisc(true)
                .bitmapConfig(Bitmap.Config.RGB_565) // default
                .displayer(new SimpleBitmapDisplayer())
                .imageScaleType(ImageScaleType.EXACTLY)
                .build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
                .threadPriority(Thread.MAX_PRIORITY).threadPoolSize(5)
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new UsingFreqLimitedMemoryCache(6 * 1024 * 1024))
                .discCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .build();

        ImageLoader.getInstance().init(config);

        Object repeat = MyArrList.get(position).get("Repeat");

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int height = metrics.heightPixels;
        int width = metrics.widthPixels;

        //Try when row = null
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = inflater.inflate(R.layout.activity_column, null);

            holder = new ViewHolder();

            holder.imageView = (ImageView) convertView.findViewById(R.id.ColImgPath);

            holder.tvUsername = (TextView) convertView.findViewById(R.id.tVforename);
            holder.tvUserSurname = (TextView) convertView.findViewById(R.id.tVsurname);
            holder.ivProfilePhoto = (ImageView) convertView.findViewById(R.id.pphoto);

            holder.tvNameAcc = (TextView) convertView.findViewById(R.id.N);
            holder.tvBrandAcc = (TextView) convertView.findViewById(R.id.B);
            holder.tvPriceAcc = (TextView) convertView.findViewById(R.id.P);
            holder.tvShopAcc = (TextView) convertView.findViewById(R.id.S);

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

        String url = (MyArrList.get(position).get("ImageThumBitmap").toString());

        // ColImage
        holder.imageView.setPadding(5, 5, 5, 5);
        //holder.imageView.setScaleType(ImageView.ScaleType.CENTER);
        holder.imageView.setRotation(90);

        //Depends on the photo show or hidden the name and surname
        if(repeat.equals("1")){

            holder.imageView.setVisibility(View.VISIBLE);
            holder.tvUserSurname.setVisibility(View.VISIBLE);
            holder.tvUsername.setVisibility(View.VISIBLE);
            holder.tvUserSurname.setText(MyArrList.get(position).get("UserSurname").toString());
            holder.tvUsername.setText(MyArrList.get(position).get("UserName").toString());

            try {
                //Bitmap bmp = ((Bitmap) MyArrList.get(position).get("ImageThumBitmap"));

                //int size = Math.min(bmp.getWidth(), bmp.getHeight());
                //int x = (bmp.getWidth() - size) / 16;
                //int y = (bmp.getHeight() - size) / 9;
                //Bitmap result = bmp.createBitmap(bmp, x, y, size, size);

                //Bitmap photobitmap1 = Bitmap.createScaledBitmap(bmp, width/2, height/2, true);
                //holder.imageView.setImageBitmap(bmp);

                ImageLoader.getInstance().displayImage(url, holder.imageView);

                //holder.imageView.setImageBitmap((Bitmap) MyArrList.get(position).get("ImageThumBitmap"));

            } catch (Exception e) {
                // When Error
                holder.imageView.setImageResource(android.R.drawable.ic_menu_report_image);
            }

        }
        else {
            //If the photo_id is repeated these params are not shown
            holder.tvUserSurname.setVisibility(View.GONE);
            holder.tvUsername.setVisibility(View.GONE);
            holder.imageView.setVisibility(View.GONE);

        }

        // ColNameAcc
        holder.tvNameAcc.setPadding(20, 0, 0, 0);
        holder.tvNameAcc.setText(MyArrList.get(position).get("NameAcc").toString());

        // ColBrandAcc
        holder.tvBrandAcc.setPadding(20, 0, 0, 0);
        holder.tvBrandAcc.setText(MyArrList.get(position).get("BrandAcc").toString());

        // ColPriceAcc
        holder.tvPriceAcc.setPadding(20, 0, 0, 0);
        holder.tvPriceAcc.setText(MyArrList.get(position).get("PriceAcc").toString() 

        // ColShopAcc
        holder.tvShopAcc.setPadding(20, 0, 0, 0);
        holder.tvShopAcc.setText(MyArrList.get(position).get("Shopname").toString());

        return convertView;

    }

}

public static class ViewHolder {
    ImageView imageView;
    TextView tvUsername;
    TextView tvUserSurname;
    ImageView ivProfilePhoto;

    TextView tvNameAcc;
    TextView tvBrandAcc;
    TextView tvPriceAcc;
    TextView tvShopAcc;
}

}

Logcat错误:

05-20 13:52:50.496    6524-3500/com.AndroidUK.SocialApp.app E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #4
Process: com.AndroidUK.SocialApp.app, PID: 6524
java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:300)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:841)
 Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
        at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6094)
        at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:824)
        at android.view.View.requestLayout(View.java:16455)
        at android.view.View.requestLayout(View.java:16455)
        at android.view.View.requestLayout(View.java:16455)
        at android.view.View.requestLayout(View.java:16455)
        at android.view.View.requestLayout(View.java:16455)
        at android.view.View.requestLayout(View.java:16455)
        at android.widget.AbsListView.requestLayout(AbsListView.java:1916)
        at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:814)
        at android.widget.AbsListView$AdapterDataSetObserver.onChanged(AbsListView.java:6280)
        at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
        at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
        at com.AndroidUK.SocialApp.app.Listview$LoadContentFromServer.doInBackground(Listview.java:212)
        at android.os.AsyncTask$2.call(AsyncTask.java:288)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)

在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587) 在java.lang.Thread.run(Thread.java:841)

另外我有一个问题是刷新我的listview,因为数据来自数据库,我不知道如何通知有新照片。有时应用关闭,因为我上传了一张新照片。当我上传照片而没有关闭应用程序时,我会查看列表视图。

由于

1 个答案:

答案 0 :(得分:0)

您正在非主线程中调用 imageAdapter.notifyDataSetChanged(),例如 onPostExecute() onPostExecute()是执行UI操作,更新等的方法。 doInBackground(),正如名称所示,它只执行后台操作,例如从中访问数据文件,网络等