gpt4 book ai didi

java - 想要将 ArrayList> 转换为字符串

转载 作者:行者123 更新时间:2023-12-02 00:04:29 25 4
gpt4 key购买 nike

我读了Universal Image Loader我制作了具有 gridview 的应用程序,并且该 gridview 从 json 获取图像。但我的应用程序加载速度很慢,所以我想使用通用图像加载器,但是 getView ImageAdapeter 类中通用图像加载器中的函数,它的使用方式如下

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

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

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

请尽可能确认或建议或推荐我。易于阅读my code

下面是我的 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;
}
}

最佳答案

如果您想保留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 中

关于java - 想要将 ArrayList<HashMap<String, Object>> 转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14153674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com