在gridview中加载延迟

时间:2012-12-27 09:42:43

标签: android json gridview imageview lazy-loading

这是从json获取图像的gridview。我顺利完成了它。但是当我从服务器上获取它时,它有很多图像,这意味着来自服务器的所有图像。我想一次加载是12张图片。

如何限制要加载的图像数量?

请简要帮我一下,因为我懒得加载。如果下面的代码不够用。这里是完整的来源http://pastie.org/5583485

在这个网址中,50是要加载的图像数量,我可以通过此值限制图像数量,无论我想要什么。但我只能加载一次,所以我无法加载它。此URL提供图像路径为json。

String url = "http://xxx.xxx.xxx/card/all/50/0/?token=57LzEsmBeykLbDCD04wTgK9WWV2XjJY0XBdqVU0HQvjIdu5EtTWOT1IQ1AYNwxt6Q5bG6FG73uvzLQSDGAIezwc8VcopEp0s63uzbdVgLSfts0TLmuVDOgyfn4lX";

asynctask的doInBackground

public class DownloadJSONFileAsync extends AsyncTask<String, Void, Void> {

    protected void onPreExecute() {
        super.onPreExecute();
        showDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);
    }

    @Override
    protected Void doInBackground(String... params) {

        String url = "http://rest.mcolle.com/card/all/50/0/?token=57LzEsmBeykLbDCD04wTgK9WWV2XjJY0XBdqVU0HQvjIdu5EtTWOT1IQ1AYNwxt6Q5bG6FG73uvzLQSDGAIezwc8VcopEp0s63uzbdVgLSfts0TLmuVDOgyfn4lX";
        JSONArray data = null;

        try {

            JSONObject jsonObject = new JSONObject(getJSONUrl(url));

            MyArrList = new ArrayList<HashMap<String, Object>>();
            HashMap<String, Object> map;
            data = jsonObject.getJSONArray("data");
            for (int i = 0; i < data.length(); i++) {
                JSONObject c = data.getJSONObject(i);
                map = new HashMap<String, Object>();

                // Thumbnail Get ImageBitmap To Object
                map.put("cardimage", (String) c.getString("cardimage"));
                map.put("ImageThumBitmap",(Bitmap) loadBitmap(c.getString("cardimage")));

                map.put("categoryid", (String) c.getString("categoryid"));

                MyArrList.add(map);

            }

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

        return null;
    }

    protected void onPostExecute(Void unused) {
        ShowAllContent(); // When Finish Show Content
        dismissDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);
        removeDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);
    }

ImageAdpater,其中我显示图像

public class ImageAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<HashMap<String, Object>> MyArr = new ArrayList<HashMap<String, Object>>();

    public ImageAdapter(Context c, ArrayList<HashMap<String, Object>> myArrList) {
        context = c;
        MyArr = myArrList;
    }

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

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        viewHolder = new ViewHolder();

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.grid_item, null);
        }
        viewHolder.categoryCard = (ImageView) convertView.findViewById(R.id.category_card);

            viewHolder.categoryCard.setImageResource(R.drawable.card_etc);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView1);

        try {
            viewHolder.imageView.setImageBitmap((Bitmap) MyArr.get(position).get("ImageThumBitmap"));
        } catch (Exception e) {
            viewHolder.imageView.setImageResource(android.R.drawable.ic_menu_report_image);
        }

        return convertView;

    }

}

1 个答案:

答案 0 :(得分:1)

你可以使用通用图像加载器,

https://github.com/nostra13/Android-Universal-Image-Loader