如何在Map<>中获取值?

时间:2017-01-03 11:34:30

标签: java android baseadapter

我试图在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);
            }
        }
    }

使用上面的代码,我可以获得&#34; effects_id&#34;:&#34; 1&#34;,&#34; effects_name&#34;:&#34; Band 1&#34

如何获取&#34; 1&#34;,&#34; 2&#34;的 effect_id和effect_name 和&#34; 3&#34;

3 个答案:

答案 0 :(得分:0)

您可以从 getItem 方法获取第一个位置的EffectList,并获取&#34; 1&#34;,&#34; 2&#34;的effect_id和effect_name的数据。和&#34; 3&#34;。

喜欢:

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

来自effectListFor1,您可以获得所需的值。

答案 1 :(得分:0)

使用以下代码!

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库! 只需在app level build.gradle中编写compile 'com.google.code.gson:gson:2.6.2'

答案 2 :(得分:0)

更具特色性

 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
        }