1

这是从 json 获取图像的 gridview。我成功地使用了它。但是当我从服务器获取它时,它有很多图像,这意味着所有图像都来自服务器。我想一次加载 12 张图像。

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

请简要帮助我,因为我对延迟加载视而不见。如果下面的代码还不够。这是完整的来源http://pastie.org/5583485

在这个 url 中,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);
    }

我显示图像的 ImageAdapter

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;

    }

}
4

1 回答 1

1

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

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

于 2012-12-27T09:48:56.090 回答