java - 如何将hashmap中的数据从json获取到适配器中?

标签 java android hashmap

我尝试将 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/

相关文章:

java - 在 Tomcat 中启用飞行记录器会导致 "Error when initializing JFR"

javascript - 滚动仅在 Android Chrome 上不起作用

android - 如果它包含一个在混淆器中带有注释的字段,如何保持类的成员不受影响

c++ - C++中哈希表的实现

java - 将自己的类设置为 java Hashmap 中的键

java - 自定义权限评估器

java - jackson :将json的一些字段映射到类的内部字段

java - 底部导航栏(布局)通过键盘向上移动

java - 有没有更好的方法在 Android 中显示 %

Java - 随机数生成器中的缺陷