【问题标题】:How to get values in Map<>?如何在 Map<> 中获取值?
【发布时间】:2017-01-03 11:34:30
【问题描述】:

我正在尝试在 Android 中使用 Map 获取以下 Json。

"effect_list": [{
      "1":[  
         {  
            "effects_id":"1",
            "effects_name":"Band 1"
         },
         {  
            "effects_id":"2",
            "effects_name":"Band 2"
         },
         {  
            "effects_id":"3",
            "effects_name":"Band 3"
         }
      ],
      "2": [ 
         {  
            "effects_id":"4",
            "effects_name":"Background Blur"
         },
         {  
            "effects_id":"5",
            "effects_name":"Blemish Removal"
         }
      ],
     "3": [ 
         {  
            "effects_id":"6",
            "effects_name":"Background Blur"
         },
         {  
            "effects_id":"7",
            "effects_name":"Blemish Removal"
         }
      ]
   }]
}

我正在尝试使用 BaseAdapter 在 Listview 中显示 Map 的值。我可以使用 getItem(position).get("1").get(0); 的键成功获取特定值。如何获取地图中的所有值?

 public class MyContactAdapter2 extends BaseAdapter {
        List<Map<String, List<EffectList>>> contactList;
        Context context;
        private LayoutInflater mInflater;
        int size = 0;
        ViewHolder vh;

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


            EffectList item = getItem(position).get("1").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);
            }
        }
    }

使用上面的代码我可以得到 "effects_id":"1", "effects_name":"Band 1"

如何获取“1”、“2”和“3”的effect_id和effect_name

【问题讨论】:

标签: java android baseadapter


【解决方案1】:

您可以通过getItem方法获取第一个位置的EffectList,并获取“1”、“2”和“3”的effect_id和effect_name的数据。

喜欢:

Map<String, List<EffectList>> itemListByNumber = myContactAdapter2.getItem(0);
List<EffectList> effectListFor1 = itemListByNumber.get("1");

从 effectListFor1 你可以得到所需的值。

【讨论】:

    【解决方案2】:

    使用以下代码!

    JsonArray jsonArray = get Jsonarray with key "effect_list"
        for (int i=0; i < jsonArray.size(); i++ ){
             JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();
             for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet())
             {
               String key =  entry.getKey(); // will return the 1 for i ==0
               if (entry.getValue().isJsonObject()){
                  JsonArray values =  entry.getValue().getAsJsonArray();
                  for (int j=0; j < values.size(); j++ ){
                     String effects_id =  values.get(j).getAsJsonObject().get("effects_id").getAsString(); // will return the 1 for j ==0
                     String effects_name =  values.get(j).getAsJsonObject().get("effects_name").getAsString(); // will return the Band 1 for j ==0
                  }
               }
            }
        }
    

    我正在使用 Gson 库! 只需在应用级 build.gradle 中写 compile 'com.google.code.gson:gson:2.6.2'

    【讨论】:

      【解决方案3】:

      更具体

       Map<String, List<EffectList>> itemListByNumber = myContactAdapter2.getItem(0);
          for(Map.Entry<Integer, List<EffectList>> s : itemListByNumber.entrySet())
              System.out.print(s.getKey());
              for (int i=0; i < s.getValue().size(); i ++{
                 // get the data one by one
              }
      

      【讨论】:

        猜你喜欢
        • 2020-11-27
        • 1970-01-01
        • 1970-01-01
        • 2020-07-14
        • 2020-04-23
        • 1970-01-01
        • 2013-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多