java - 仅 ListView 中的最后一项显示在 BaseAdapter 中

标签 java android 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);
            }
        }
    }

最佳答案

问题出在这部分代码:

        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());

            }
        }

对于每个列表项,您将依次设置键和值。所以在 for 循环结束时,只剩下最后一个。

例如:联系人列表 - {you, me, dupree} 你正在做的是

  1. vh.textViewName(您)
  2. vh.textViewName(我)
  3. vh.textViewName(dupree)

因此您将始终在列表中看到 dupree。

<小时/>

不要使用 contactList 创建 MyContactAdapter2,而是传递 Map<String, List<EffectList>> map直接

如果您只想在列表中显示textViewName 和textViewEmail。然后创建包含姓名和电子邮件的对象列表。

class Contact {
public String name;
public String email;
}

List<Contact> contacts;

现在将此联系人传递给适配器。保持简单。

关于java - 仅 ListView 中的最后一项显示在 BaseAdapter 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41459239/

相关文章:

java - 查询 OpenStreetMap API 的 map 特征

android - 如何在 Ubuntu 中使用终端关闭 Android Studio

java - BaseAdapter 只让我在 ListView Android 中排在第一位

android - AsyncTask 完成时强制 BaseAdapter 更新其 View

Android ListView : Is it possible to add data to a few rows before-wards, 而 ListView 的其余部分是动态加载的吗?

java - 获取 JSP servlet session ,返回空值

java - 如何定义方法compareTo()?

java - 是否是 StringBuilder 错误?

java - 使用 robolectric 的 Android 测试套件?

android - 在 Android 上构建兼容性 PreferenceFragment