Search code examples
androidarraylistgoogle-cloud-firestorespinner

Add two ArrayList on single spinner


Refer the image
I want to match elements of favourite_city with options and if match found set favourite_city list as topmost position in options with its own content.

First ArrayList:

favourite_city=["Hyderabad","Bangalore"]

Second ArrayList:

options=["Nashik","Hyderabad","Mumbai","Bangalore","Chennai","Pune"]

Result:

options=["Hyderabad","Bangalore","Nashik","Hyderabad","Mumbai","Bangalore","Chennai","Pune"]

    DocumentReference docRef5 = FirebaseFirestore.getInstance().collection("admin").document("users_profile").collection("dynamic_profile").document("city");
    docRef5.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                DocumentSnapshot document = task.getResult();
                if (document != null) {

                    //-----------code for loading array item from firebase to spinner view-----------------
                    final List<String> group = (List<String>) document.get("favourite_city");
                    Log.d(TAG, "favourite city list data: " + group);

                    final List<String> group1 = (List<String>) document.get("options");
                    Log.d(TAG, "options list data: " + group1);

                    cityAdapter = new ArrayAdapter<String>(AdminSetting.this, android.R.layout.simple_spinner_item, group1);
                    cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    citySpinner.setAdapter(cityAdapter);


                    StringBuilder str = new StringBuilder();
                    for (int i = 0; i < group.size(); i++) {
                        if (i >= 0)
                            str = str.append(group.get(i));

                            String searchedItem = str.toString();

                            Log.d(TAG, "result " + searchedItem);

                        int itemPosition = cityAdapter.getPosition(searchedItem);

                        for(int j=0;j<=i;j++) {

                            if (itemPosition == -1) {
                                String message = searchedItem + " : Item not found.";
                                Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
                            } else {

                                String message = searchedItem + " : Item found and selected.";
                                Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
                                citySpinner.setSelection(itemPosition);
                            }
                        }

                        Log.d(TAG, "after appending " + str);
                    }
                } else {
                    Log.d(TAG, "No such document");
                }
            } else {
                Log.d(TAG, "get failed with ", task.getException());
            }
        }
    });

Solution

  • In order to put in the first positions the favourites cities, you need to know if they are contained in the cities list.

    Check this code as an example:

    ArrayList<String> favourites = new ArrayList<>(Arrays.asList("city1", "city2"));
    ArrayList<String> cities = new ArrayList<>(Arrays.asList("city3", "city4", "city1", "city2"));
    
    for (String favCity : favourites) {
       if (cities.contains(favCity)) {
           cities.add(favourites.indexOf(favCity), favCity);
       }
    }
    
    System.out.println(cities);
    

    This will print the following:

    [city1, city2, city3, city4, city1, city2]

    If you want to remove the duplicated favourites cities to keep only them in the first positions, you should add this line before adding it to the list.

    cities.remove(favCity);
    

    And it will return:

    [city1, city2, city3, city4]