Convert list to array and show in spinner in android

android spinner string-array xml
android spinner arraylist object
spinner in android - stack overflow
android spinner dropdown style example
android spinner add item programmatically
how to display selected value in spinner in android
android spinner example string array
how to set value in spinner dynamically in android

I use retrofit2 and get category list.

Model Category :

public class Category implements Serializable {   
    @SerializedName("catid")
    private int catid;
    @SerializedName("title")
    private String title;
    @SerializedName("qcount")
    private String qcount;
    public int getCatid() {
        return catid;
    }
    public void setCatid(int catid) {
        this.catid = catid;
    }    
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
}

Retrofit call and callback :

Call<List<Category>> call =  apiInterface.getCategories();
call.enqueue(new Callback<List<Category>>() {
    @Override
    public void onResponse(Call<List<Category>> call, Response<List<Category>> response) {
        hidePDialog();
        if(response.isSuccessful()){
            List<Category> cats =  response.body();
            //codes to convert cats to array.
            // i need cats title to insert in array.
        }
    }
    @Override
    public void onFailure(Call<List<Category>> call, Throwable t) {
    }
});

Spinner :

s1= findViewById(R.id.spinner);
ArrayAdapter<String> adap=new ArrayAdapter<String>
        (this, android.R.layout.simple_spinner_item, cat_title);
s1.setAdapter(adap);

How to show cats title in spinner? Example : cats :

[{    
    "catid": 1,
    "title": "windows" 
    },
    {
    "catid": 2,
    "title": "Android",
    }
]

All above codes in one activity. Please help me. Thanks in advanced. i edit activity but it dont work! what is the problem :

public class NewPostActivity extends AppCompatActivity {
    private  ArrayList<String> cat_title;
    Spinner s1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_post);
        CatsRequest();
        String[] cat_title1 = (String[]) cat_title.toArray(new String[cat_title.size()]);
        s1= findViewById(R.id.spinner);
        ArrayAdapter<String> adap=new ArrayAdapter<String>
                (this, android.R.layout.simple_spinner_item, cat_title1);
        s1.setAdapter(adap);
        s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    private void CatsRequest(){

        APIInterface apiInterface = APIClient.getClient().create(APIInterface.class);
        Call<List<Category>> call =  apiInterface.getCategories();
        call.enqueue(new Callback<List<Category>>() {
            @Override
            public void onResponse(Call<List<Category>> call, Response<List<Category>> response) {
                hidePDialog();
                if(response.isSuccessful()){
                    List<Category> cats =  response.body();
                    ArrayList<String> cat_title= new ArrayList<String>();
                    for(Category cat: cats){
                        cat_title.add(cat.getTitle());
                    }
                }
            }
            @Override
            public void onFailure(Call<List<Category>> call, Throwable t) {
            }
        });
    }
}

Failed to run. error : Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference


Unless i misunderstood something, all you have to do is:

    for(Category c: cats)
    {
        yourarray.Add(c.getTitle());
    }

How to set Android Spinner (drop down) array data from Java code , How to set Android Spinner (drop down) array data from Java code shows how to set the data (array) on an Android Spinner (also known as a dropdown set the spinner data programmatically, from a string array or list // (1) get a reference How to convert an Android CursorWrapper into a List/ArrayList  As a quick note, this source code shows how to set the data (array) on an Android Spinner (also known as a dropdown list, or drop down list) from Java code: // set the spinner data programmatically, from a string array or list // (1) get a reference to the spinner Spinner spinner1 = (Spinner)qbTableLayout.findViewById(R.id.spinner1); // (2


List<Category> cats =  response.body();
ArrayList<String> titles= new ArrayList<String>();
ArrayList<Integer> ids= new ArrayList<Integer>();//in case you need the ids too
for(Category cat: cats){
    titles.add(cat.getTitle());
    ids.add(cat.getId());
}

Attaching the adapter to the spinner:

 ArrayAdapter<String> adp = new ArrayAdapter<String>(this,
                          android.R.layout.simple_list_item_1, list);
 adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 sp.setAdapter(adp1);//sp is the spinner obj

In case you need to compute the value of the id based on the title

   sp.setOnItemSelectedListener(new OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
          int id = ids.get(position);//This will be the student id.
      }

      @Override
      public void onNothingSelected(AdapterView<?> parentView) {
        // your code here
      }

    });

Happy coding.

How can I add items to a spinner in Android?, Spinner is just like a drop down button, using this button we can select a item from set of items. How to programmatically turn on Wifi on Android device? In the above code we are adding item to array list and added to ArrayAdpter as shown below. Now select any item, it will show as shown below. Spinner in Android. Spinner Example in Android. In this tutorial, we will code an example Android app with a Spinner drop down implemented, with two inter dependent Spinners in which options of one spinners depends on the value selected in the other spinner.


Ctegory[] categoryArray = cats.toArray(new Category[cats.size()]);

Spinners, Overview · Numerical types · Object types · Conversion functions In the default state, a spinner shows its currently selected value. To populate the spinner with a list of choices, you then need to your spinner are pre-determined, you can provide them with a string array defined in a string resource file:. Define spinner used in activity by same id and arrayadapter for list the stored String in array final Spinner spinner = (Spinner) findViewById(R.id.spinner1); ArrayAdapter adapter = ArrayAdapter.createFromResource( this, R.array.countries, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner


ArrayList, The new elements will appear in the list in the order that they are returned by the specified collection's iterator. Parameters. index, int : index at  A data source of a Spinner can be data fetched from a local SQLite or a remote Mysql database, an array of strings, an ArrayList object, or an string array defined in xml layout file. In this Android tip, I am going to show to how to define a string array in xml file and how to read items of the array to display in a Spinner widget.


Android Spinner - drop down list, Android Spinner Example, Android Drop Down List, android studio spinner, android Following xml file shows the layout of a typical spinner in android which ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android. Android Platform. API level 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1. Manifest.permission. Manifest.permission_group. android.accessibilityservice. AccessibilityService.MagnificationController.OnMagnificationChangedListener. AccessibilityService.SoftKeyboardController.OnShowModeChangedListener.


Android Spinner (Dropdown List) with Examples, By default, the android spinner will show its currently selected value and by data from sources such as an array or database and converts each item into a  Show ListView as Dropdown in Android Complete Code. MainActivity.java – shows the button, set the items for the drop down list, creates the pop up window and then show it as drop down when the button was touched.