java - 如何在 BaseAdapter 的 GetView() 方法中迭代 Map 值?

标签 java android android-adapter baseadapter

我正在尝试解析以下 Json。

{
"effect_list": [{
      "1":[  
         {  
            "effects_id":"1",
            "effects_name":"Band 1"
         },
         {  
            "effects_id":"2",
            "effects_name":"Band 2"
         }


      ],
      "2": [ 
         {  
            "effects_id":"4",
            "effects_name":"Background Blur"
         },
         {  
            "effects_id":"5",
            "effects_name":"Blemish Removal"
         }
      ] 
   }]
}

我的 BaseAdapter 仅显示 ListView 中的第一个项目。我正在尝试检索 map 中的值。在迭代期间,值被覆盖。当我打印值时,它打印正确。

MyContactAdapter2.java

public class MyContactAdapter2 extends BaseAdapter {
        List<Map<String, List<EffectList>>> contactList;
        Context context;
        private LayoutInflater mInflater;


        // Constructors
        public MyContactAdapter2(Context context, List<Map<String, List<EffectList>>> objects) {

            this.context = context;
            this.mInflater = LayoutInflater.from(context);
            contactList = objects;
        }

        public MyContactAdapter2() {
            System.out.println("hai");
        }

        @Override
        public int getCount() {
            int count = contactList.size();
            System.out.println("Count size" + count);
            return count;
        }

        @Override
        public Map<String, List<EffectList>> getItem(int position) {
            return contactList.get(position);
        }


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


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {


            System.out.println(10);


            final MyContactAdapter2.ViewHolder vh;
            if (convertView == null) {
                View view = mInflater.inflate(R.layout.get_layout_row_view, parent, false);
                vh = MyContactAdapter2.ViewHolder.create((RelativeLayout) view);
                view.setTag(vh);

            } else {
                vh = (MyContactAdapter2.ViewHolder) convertView.getTag();
            }





            for (Map<String, List<EffectList>> map : contactList) {
                for (Map.Entry<String, List<EffectList>> entry : map.entrySet()) {
                    String key = entry.getKey();
                    Object value = entry.getValue();
                    System.out.println("key :" + key + "value :" + value);

                        EffectList item = getItem(position).get(key).get(0);

                        vh.textViewName.setText(item.getEffectsId());
                        vh.textViewEmail.setText(item.getEffectsName());

                }
            }


            return vh.rootView;
        }


        private static class ViewHolder {
            public final RelativeLayout rootView;
            public final ImageView imageView;
            public final TextView textViewName;
            public final TextView textViewEmail;

            private ViewHolder(RelativeLayout rootView, ImageView imageView, TextView textViewName, TextView textViewEmail) {
                this.rootView = rootView;
                this.imageView = imageView;
                this.textViewName = textViewName;
                this.textViewEmail = textViewEmail;
            }

            public static MyContactAdapter2.ViewHolder create(RelativeLayout rootView) {
                ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView);
                TextView textViewName = (TextView) rootView.findViewById(R.id.textViewName);
                TextView textViewEmail = (TextView) rootView.findViewById(R.id.textViewEmail);
                return new MyContactAdapter2.ViewHolder(rootView, imageView, textViewName, textViewEmail);
            }
        }
    }

所以我的问题是,如何迭代“BaseAdapter”中“GetView()”内的“Map”值?

是否可以在 BaseAdapter 的 GetView() 方法中迭代值?

最佳答案

你不能这样做吗:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // ... blah blah ...

    final Map<String, List<EffectList>> effectMap = getItem(position);

    for (Map.Entry<String, List<EffectList>> entry : effectMap.entrySet()) {
        final String key = entry.getKey();
        final List<EffectList> value = entry.getValue();
        // ... do stuff ...
    }

    // ... blah blah 
}

关于java - 如何在 BaseAdapter 的 GetView() 方法中迭代 Map 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41478714/

相关文章:

android - 如何使用 GridLayoutManager 设置每行不同的列数

java - JCA 资源适配器如何读取 ra.xml 中定义的自定义属性

java - 在Android中从/向文件读取/写入字符串

java - 为什么这个 Java 程序的第 17 行没有被执行?

java - 如何在android中使底部导航栏不透明?

java - fragment View 上 AsyncTask 中的 setContentView() 问题

android - 发布版本上的 INSTALL_PARSE_FAILED_NO_CERTIFICATES

android - Android 适配器中的错误 java.lang.IndexOutOfBoundsException : Invalid index 0, 大小为 0

java - 指向一个hashSet java

android - 如何从适配器访问 ListView