0

I am trying to get the following Json using Map in Android.

"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"
         }
      ]
   }]
}

I am trying to display the values of Map in Listview using BaseAdapter. I can successfully get a particular value using it's key by getItem(position).get("1").get(0);. How can I get all the values in the Map?

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

Using the above code I am able to get "effects_id":"1", "effects_name":"Band 1"

How to get effect_id and effect_name of "1","2" and "3"

user7357013
  • 126
  • 3
  • 9
  • 3
    Possible duplicate of [How to get values. keys from HashMap?](http://stackoverflow.com/questions/16246821/how-to-get-values-keys-from-hashmap) – Manza Jan 03 '17 at 11:44

3 Answers3

0

You can get the EffectList for first position from getItem method and get the data for effect_id and effect_name of "1","2" and "3".

Like :

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

from effectListFor1 you can get the required values.

Rohit Gulati
  • 542
  • 3
  • 15
0

use the following codes!

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

I am using Gson library! just write compile 'com.google.code.gson:gson:2.6.2' in app level build.gradle

Tulsi
  • 719
  • 7
  • 15
0

To be more specfic

 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
        }
Tulsi
  • 719
  • 7
  • 15