Как получить данные из hashmap в адаптер из json? - PullRequest
0 голосов
/ 23 марта 2020

Я пытаюсь получить данные из 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;
    }
}

Мой хэш-карта:

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

1 Ответ

0 голосов
/ 23 марта 2020

Думаю, ваша проблема в методах 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
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...