我尝试将 HashMap 中的数据获取到我的 CustomArrayAdapter 中,我的 HashMap 的数据是键,值对是类型,图像根据类型,我想在单击项目 gridview 时获取图像。 我的适配器有问题。 提前致谢。 这是我的适配器。
Adapter_subCategories.java
public class Adapter_subCategories extends BaseAdapter {
LayoutInflater inflater;
Context context;
ArrayList<HashMap<String,String>> wallpaperArrayList;
public Adapter_subCategories(LayoutInflater inflater, Context context, ArrayList<HashMap<String,String>> array) {
this.inflater = inflater;
this.context = context;
this.wallpaperArrayList = array;
}
public Adapter_subCategories(Item_categories_show item_categories_show, ArrayList<HashMap<String, String>> mapArrayList) {
}
public Adapter_subCategories(Item_categories_show item_categories_show, HashMap<String, ArrayList<Integer>> arrayListHashMap) {
}
@Override
public int getCount() {
return wallpaperArrayList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater==null)
{
inflater = LayoutInflater.from(context);
}
if (convertView==null){
convertView = inflater.inflate(R.layout.list_images_subcategories,null);
}
ImageView imageView = convertView.findViewById(R.id.imageView_subcategories);
HashMap<String,String> maps = new HashMap<String, String>();
maps = wallpaperArrayList.get(position);
// this is my trouble
String type = maps.get("rootType");
String image = maps.get("url");
// maps = wallpaperArrayList.get(position);
imageView.setImageResource(Integer.parseInt(maps.get("url")));
return convertView;
}
}
我的 HashMap :
for (int j = 0; j<subArray.length() ; j++) {
JSONObject obj = subArray.getJSONObject(j);
JSONObject row = obj.getJSONObject("row");
String url = row.getString("url");
HashMap<String,String> map = new HashMap<String, String>();
map.put(rootType,url);
hashMapArrayList.add(map);
我的json:http://www.vnsupa.com/dulieujson/data.json
最佳答案
我认为您的问题出在 getItem() 和 getItemId() 这两个方法中。更改两个方法签名,例如
@Override
public Object getItem(int position) {
return wallpaperArrayList.get(position);//return your object
}
@Override
public long getItemId(int position) {
return position;//return position
}
关于java - 如何将hashmap中的数据从json获取到适配器中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60810732/