ListView只显示一个项

时间:2017-01-06 05:31:17

标签: java android listview baseadapter

我试图在ListView中显示Json。我正在使用Map来检索值。列表中仅显示第一个项目。 BaseAdapter中的 getCount()每次返回大小为1,即使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);
            }
        }
    } 

我在迭代地图时做错了什么?

这是我试图解析的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"
         }
      ] 
   }]
}

我想显示1(0)和2(0)。只有1(0)(&#34; effects_id&#34;:&#34; 1&#34;)显示在ListView

为什么我的getCount()大小始终为1,即使它有多个值?

4 个答案:

答案 0 :(得分:4)

试试这个,

public class MyContactAdapter2 extends BaseAdapter {
    List<EffectList> contacts;
    Context context;
    private LayoutInflater mInflater;


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

    this.context = context;
    this.mInflater = LayoutInflater.from(context);
    this.contacts = new ArrayList<>();
    Map<String, List<EffectList>> map = objects.get(0);
    Set<String> keySet = map.keySet();
    Iterator<String> iterator = keySet.iterator();
    while (iterator.hasNext()) {
        this.contacts.addAll(map.get(iterator.next()));
    }
}

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

    @Override
    public int getCount() {
        int count = contacts.size();
        return count;
    }

    @Override
    public EffectList getItem(int position) {
        return contacts.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();
        }
            EffectList item = getItem(position);

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

答案 1 :(得分:1)

我认为在列表中添加了更好的方式

    ArrayList<EffectList> data = new ArrayList<>();


     for (Map<String, List<EffectList>> map : contactList) {
                    for (Map.Entry<String, List<EffectList>> entry : map.entrySet()) {
                        String key = entry.getKey();
                        List<EffectList> value = entry.getValue();
                        data.addAll(value);
                    }
                }

MyContactAdapter2  mAdpter = new MyContactAdapter2(this,data);
list.setAdapter(mAdpter);

然后在Adapter

中使用data列表
public class MyContactAdapter2 extends BaseAdapter {
        ArrayList<EffectList> contactList;
        Context context;
        private LayoutInflater mInflater;


        // Constructors
        public MyContactAdapter2(Context context, ArrayList<EffectList> data ) {

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

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

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

        @Override
        public 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();
            }

                        EffectList item = getItem(position);

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



            return vh.rootView;
        }

答案 2 :(得分:0)

您的列表视图仅显示一个项目,因为您只有一个项目。 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"
    }
  ]
}

答案 3 :(得分:0)

让您的 json 如下所示并相应地使用。

&#13;
&#13;
{
"effect_list": [
     
         {  
            "effects_id":"1",
            "effects_name":"Band 1"
         },
         {  
            "effects_id":"2",
            "effects_name":"Band 2"
         },
         {  
            "effects_id":"4",
            "effects_name":"Background Blur"
         },
         {  
            "effects_id":"5",
            "effects_name":"Blemish Removal"
         }
     
   ]
}
&#13;
&#13;
&#13;

您可以浏览 effect_list.size 并获取每个值