0

我阅读了通用图像加载器,并制作了具有 gridview 的应用程序,并且这个 gridview 从 json 获取图像。但是我的应用程序加载速度很慢,所以我想使用通用图像加载器,但getView它在 ImageAdapeter 类中的通用图像加载器中运行它就像这样使用

imageLoader.displayImage(imageUrls[position], imageView, options);或公式 imageLoader.displayImage(String, imageView, options);

所以我没有一个字符串来完成这个公式。我只是可以创建Object arr来存储 Arraylist。

如何转换ArrayList<HashMap<String, Object>>为字符串或以任何方式更改我的代码?

请尽可能确认或建议或推荐我。易于阅读我的代码

下面是我的 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);
        //==================== change area
        Object arr = new ArrayList<HashMap<String, Object>>(); // make objec arr to store arraylist
        arr = MyArr.get(position).get("categoryid");
        if (arr.equals("1")) {
            viewHolder.categoryCard.setImageResource(R.drawable.card_eat);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView1);
        } else {
            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;

    }

}


// Download JSON in Background
public class DownloadJSONFileAsync extends AsyncTask<String, Void, Void> {
    String token = getIntent().getExtras().getString("token1");

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

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

        String url = "http://xxx.xxx.xxx/card/all/20/0/?token="+token;
        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);
    }

}

这是来自Universal Image Loader的 ImageAdapter

public class ImageAdapter extends BaseAdapter {
    @Override
    public int getCount() {
        return imageUrls.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ImageView imageView;
        if (convertView == null) {
            imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
        } else {
            imageView = (ImageView) convertView;
        }
                     // I want to do like this
        imageLoader.displayImage(imageUrls[position], imageView, options);

        return imageView;
    }
}
4

2 回答 2

2

如果你想保留ArrayList<HashMap<String, Object>>你必须以这种方式创建一个新的String[]

String[] urls = new String[MyArrList.size()];
for(int i=0; i<MyArrList.size(); i++){
   urls[i] = MyArrList.get(i).get("cardimage");
}

PS。我已经使用了您在代码中声明的变量名 MyArrList,顺便说一下,以大写字母开头的变量名不是一个好主意。

PS2。我认为如果您想使用通用图像加载器,您可能不再需要将每个位图存储在 HashMap

于 2013-01-04T09:04:38.913 回答
1

在您DownloadJSONFileAsync Class这里发布的内容中,

  1. 您尚未下载 JSON。因此,您的变量JSONArray data将始终为空。
  2. Line 33不需要使用new ArrayList您需要访问存储在 ArrayList 中的情况下DownloadJSONFileAsync Class。在下面找到更新的代码。

    public class DownloadJSONFileAsync extends AsyncTask<String, Void, Void> {
    
    String token = getIntent().getExtras().getString("token1");
    JSONObject response = null;
    
    protected void onPreExecute() {
        super.onPreExecute();
        showDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);
    }
    
    @Override
    protected Void doInBackground(String... params) {
    
        String url = "http://xxx.xxx.xxx/card/all/20/0/?token="+token;
        JSONArray response = //YOUR JSON ARRAY AFTER DOWNLOADING;
        return null;
    }
    
    protected void onPostExecute(Void unused) {
        ShowAllContent(JSONArray); // When Finish Show Content and send the Response JSONARRAY and display the content on the GridView
        dismissDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);
        removeDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);
    }}
    

您的 ShowAllContent(JSONArray jsonVal) 代码

public void ShowAllContent(JSONArray jsonVal) {

    gridView1 = (ScrollableGridView) findViewById(R.id.grid_all);
    gridView1.setAdapter(new ImageAdapter(CardActivity.this, jsonVal));
    gridView1.setExpanded(true);
    gridView1.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
        }
    });

    gridView1.setOnScrollListener(new PauseOnScrollListener(true, true));

}

在您getView() method of ImageAdapter使用以下行。

public class ImageAdapter extends BaseAdapter {
    private JSONArray jsonArray = null;
    public ImageAdapter(Context c, JSONArray jsonArray) {
        context = c;
        this.jsonArray = jsonArray;
    }
    public int getCount() {
        return jsonArray.length();
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        //DO YOUR CODE HERE
       imageLoader.displayImage(jsonArray[position].get("ImageThumBitmap").toString(), imageView, options);
    }
}
于 2013-01-04T08:58:32.817 回答