如何在ListView中显示Map值?

时间:2017-01-06 09:50:24

标签: java android listview baseadapter

我正在尝试使用BaseAdapter在ListView中显示Map值。使用以下代码,我可以成功打印Map中的所有值。但我不知道如何使用其密钥显示Map的单个值。

MyContactAdapter2.java

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

        }

    }


    @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) {


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

这是我需要解析的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”(“effects_id”:“1”,“effects_name”:“Band 1”和“effects_id”:“2”,“effects_name”:“Band 2” “)即可。我怎样才能实现它?

我也对构造函数中的代码感到困惑。如果可以,请解释以下代码,

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

修改

EffectList.java

public class EffectList {



    @SerializedName("effects_id")
    @Expose
    private String effectsId;

    @SerializedName("effects_name")
    @Expose
    private String effectsName;

    public String getEffectsId() {
        return effectsId;
    }

 /*   public void setEffectsId(String effectsId) {
        this.effectsId = effectsId;
    }
*/

    public String getEffectsName() {
        return effectsName;
    }
/*
    public void setEffectsName(String effectsName) {
        this.effectsName = effectsName;
    }
    */
}

1 个答案:

答案 0 :(得分:1)

您正在获取List<Map<String, List<EffectList>>>对象,因此您正在使用第一个索引来获取列表中的第一个地图。

之后您正在检索地图的密钥集,这是一个包含地图中可用的所有密钥的集合。

迭代器用于从地图中检索地图包含的每个键的相应列表。

您可以通过执行以下操作来优化此操作:

Set<List<EffectList>> values = map.values;
Iterator<List<EffectList>> it = values.iterator();

while(it.hasNext() {
    this.contacts.addAll(it.next());
}

要存档所需的输出,您可以在将此列表添加到联系人列表之前过滤效果列表。

修改

例如(使用google commons集合):

List<EffectList> itValue = it.next();
List<EffectList> filteredValue = Collections.filter(itValue, new Predicate<EffectList>(){
   public boolean apply(EffectList input){
      if(input.getEffectsId().equals("1") || input.getEffectsId().equals("2"){
        return true;
      }
      return false;
   }
);
this.contacts.addAll(filteredValue);