MaterialSpinner icon indicating copy to clipboard operation
MaterialSpinner copied to clipboard

How to set items from custom ArrayList

Open bviral opened this issue 7 years ago • 3 comments

Hello Sir,

I am getting data directly from db so I am having that data in custom ArrayList not in string type ArrayList. I don't want to convert it in single column List so please tell me how to set items by specifying custom type arraylist column.

bviral avatar Nov 22 '18 13:11 bviral

You need to create an Adapter of MaterialSpinnerAdapter Example you have:

 ArrayList<Country> countryArrayList = new ArrayList<>();
        Country country = new Country("Kenya",254);
        countryArrayList.add(country);
        Country country1 = new Country("Nigeria",244);
        countryArrayList.add(country1);
       MaterialSpinnerAdapter<County> countyMaterialSpinnerAdapter;
        countyMaterialSpinnerAdapter = new MaterialSpinnerAdapter<County> 
       (getContext(),countyArrayList);
        MaterialSpinner countrySpinner = view.findViewById(R.id.countrySpinner);
        countrySpinner.setAdapter(countryMaterialSpinnerAdapter);

        countrySpinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<Country>() {

            @Override public void onItemSelected(MaterialSpinner view, int position, long id, Country country) {
                Snackbar.make(view, "Clicked " + country.getName() + " code = "+country.getCode(), Snackbar.LENGTH_LONG).show();
            }
        });

ovicko avatar Apr 17 '19 11:04 ovicko

Remember the Country model class has this method

//to display object as a string in spinner
    @Override
    public String toString() {
        return name;
    }

ovicko avatar Apr 17 '19 12:04 ovicko

More reference StackOverflow

ovicko avatar Apr 17 '19 12:04 ovicko