Home > Enterprise >  Fetching multiple dictionaries into single ArrayList
Fetching multiple dictionaries into single ArrayList

Time:09-22

            String Nname = firstName.getText().toString();
            String Lname = lastName.getText().toString();
            String Mobile = mobile.getText().toString();
            String EMail = email.getText().toString();
            String DOB = dob.getText().toString();
            String Addr = addr.getText().toString();

            final HashMap<String,String> dictionary1 = new HashMap();
            dictionary1.put("firstName",Nname);
            dictionary1.put("lastName",Lname);
            dictionary1.put("Mobile",Mobile);
            dictionary1.put("Email",EMail);
            dictionary1.put("DOB",DOB);
            dictionary1.put("Address",Addr);

            ArrayList<HashMap<String, String>> Employee = new ArrayList<>();
            Employee.add(dictionary1);

            System.out.println(Employee);
            

When printed out:

Employee Details[{firstName=Sukumar, lastName=S, Email=yy@gmail.com, Address=hhh, DOB=5-7-95, Mobile=7854213799}]

For next entries it will take another separate arraylist

Employee Details[{firstName=Xyz, lastName=uu, Email=yyy, Address=yyy, DOB=yyy, Mobile=3333333}]

I need these two in a single ArrayList like

Emplyee detaills[{{firstName=Sukumar, lastName=S, Email=yy@gmail.com, Address=hhh, DOB=5-7-95, Mobile=7854213799},{firstName=Xyz, lastName=uu, Email=yyy, Address=yyy, DOB=yyy, Mobile=3333333}]

CodePudding user response:

ive tried it and it worked fine for me.

        String Nname = "Zack";
        String Lname = "Mar";
        ArrayList<HashMap<String, String>> Employee = new ArrayList<>();

        final HashMap<String,String> dictionary1 = new HashMap();
        for (int i = 0; i < 3; i  ){
            dictionary1.put("firstName",Nname);
            dictionary1.put("lastName",Lname);
            Employee.add(dictionary1);
        }


        System.out.println(Employee);

`

printes out:

[{firstName=Zack, lastName=Mar}, {firstName=Zack, lastName=Mar}, {firstName=Zack, lastName=Mar}]

Careful if you loop the whole code when you get another entry you will create a new array list here: ArrayList<HashMap<String, String>> Employee = new ArrayList<>(); and will throw the previous away.

CodePudding user response:

If above code is in setOnCLicklistener then Your logic is wrong. Because you are always initialize new array and add element on it.

Please do as follow:

ArrayList<HashMap<String, String>> employees = new ArrayList<>();

button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String Nname = firstName.getText().toString();
                    String Lname = lastName.getText().toString();
                    String Mobile = mobile.getText().toString();
                    String EMail = email.getText().toString();
                    String DOB = dob.getText().toString();
                    String Addr = addr.getText().toString();

                    final HashMap<String,String> dictionary1 = new HashMap();
                    dictionary1.put("firstName",Nname);
                    dictionary1.put("lastName",Lname);
                    dictionary1.put("Mobile",Mobile);
                    dictionary1.put("Email",EMail);
                    dictionary1.put("DOB",DOB);
                    dictionary1.put("Address",Addr);

                    employees.add(dictionary1);

                    System.out.println(employees);
                }
            });

initialize your arrayList out side the setOnClickListner.

  • Related