1. Are you ready for the Galaxy S20? Here is everything we know so far!

Customising a spinner

Discussion in 'Android Development' started by ShamusVW, Sep 9, 2019.

  1. ShamusVW

    ShamusVW Newbie
    Thread Starter

    I am working using Fragments and I pull data from a MySQL database and put it into an ArrayList.
    I then populate the Spinner with the returned data.
    What I can't seem to figure out is how to customize that Spinner holding the data using the ArrayList I already have.

    I define a spinner in my onCreateView as"
    Code (Text):
    1.  
    2. private Spinner spinner;
    3. @Nullable
    4. @Override
    5. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    6.     View view = inflater.inflate(R.layout.fragment_training_layout, container, false);
    7.     spinner = (Spinner) view.findViewById(R.id.spinnerTrainers);
    8.  
    This is my class to extract the data, and how I am populating the Spinner:

    Code (Text):
    1.  
    2.    private class ConnectMySql extends AsyncTask<String, Void, String> {
    3.         List<String> trainers = new ArrayList<String>();
    4.         ArrayAdapter<String> adapter;
    5.  
    6.         @Override
    7.         protected void onPreExecute() {
    8.             super.onPreExecute();
    9.         }
    10.  
    11.         @Override
    12.         protected String doInBackground(String... params) {
    13.             try {
    14.                 Class.forName("com.mysql.jdbc.Driver");
    15.                 Connection con = DriverManager.getConnection(url, user, pass);
    16.  
    17.                 Statement st = con.createStatement();
    18.                 ResultSet rs = st.executeQuery("SELECT * FROM pdc_production.die_detail");
    19.                 ResultSetMetaData rsmd = rs.getMetaData();
    20.  
    21.                 while (rs.next()) {
    22.                     trainers.add(rs.getString(1).toString() + ":" + rs.getString(2).toString());
    23.                 }
    24.             } catch (Exception e) {
    25.                 e.printStackTrace();
    26.             }
    27.             return "";
    28.         }
    29.  
    30.         @Override
    31.         protected void onPostExecute(String result) {
    32.             adapter = new ArrayAdapter<String> (getContext(), android.R.layout.simple_spinner_dropdown_item, trainers);
    33. //            adapter = ArrayAdapter.createFromResource(getContext(), trainers, android.R.layout.simple_spinner_dropdown_item);
    34.             spinner.setAdapter(adapter);
    35.         }
    36.  
    37.     }
    38.  
    This line populates the Spinner
    Code (Text):
    1. adapter = new ArrayAdapter<String> (getContext(), android.R.layout.simple_spinner_dropdown_item, trainers)
    The line right beneath it (commented out)
    Code (Text):
    1. adapter = ArrayAdapter.createFromResource(getContext(), trainers, android.R.layout.simple_spinner_dropdown_item)
    is the line that I can't quite figure out how to do it. The examples I have looked at show an Array being used, but I already have the ArrayList called trainers, so I don't know how to make use of it instead of creating another array.
    Could someone assist me please?

    Right now this is what my spinner looks like, very boring!

    App.JPG
     

    Advertisement

  2. wseng92

    wseng92 Android Enthusiast

  3. ShamusVW

    ShamusVW Newbie
    Thread Starter

    Thank you for the reply, only now got to looking at that link.
    I tried creating a new layout called "spinner_layout.xml" as in the example.
    I also changed my List type to an ArrayList type
    Code (Text):
    1. List<String> trainers = new ArrayList<String>();
    changed to...
    Code (Text):
    1. ArrayList<String> trainers = new ArrayList<String>();
    I am able to populate my ArrayList with items from my query, that isn't the problem.
    I just have a problem trying to alter my spinner. The Array in R.Array.spinners stays red in the code below

    Code (Text):
    1.  
    2. private class ConnectMySql extends AsyncTask<String, Void, String> {
    3. ...
    4. @Override
    5. protected void onPostExecute(String result) {
    6.     adapter = ArrayAdapter.createFromResource(getContext(), R.Array.trainers, R.layout.spinner_layout);
    7.     spinner.setAdapter(adapter);
    8. }
    9. }
    10.  
     
  4. ShamusVW

    ShamusVW Newbie
    Thread Starter

    So I figured this out, and it looks like I could make this work.
    So in my class ConnectMySql in the onPostExcute method I have the following:

    Code (Text):
    1.  
    2. @Override
    3. protected void onPostExecute(String result) {
    4.     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),R.layout.spinner_layout, trainers);
    5.     spinner.setAdapter(adapter);
    6. }
    7.  
    The only change is changing the "android.R.layout.simple_spinner_dropdown_item" to "R.layout.spinner_layout", and then creating a layout_spinner.xml file.
    This was the original line:
    Code (Text):
    1.  
    2. adapter = new ArrayAdapter<String> (getContext(), android.R.layout.simple_spinner_dropdown_item, trainers);
    3.  
    The layout_spinner.xml file:

    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3.     <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:id="@android:id/text1"
    5.     android:layout_width="match_parent"
    6.     android:layout_height="wrap_content"
    7.     android:textAppearance="?android:attr/textAppearanceListItemSmall"
    8.     android:gravity="center_vertical"
    9.     android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    10.     android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    11.     android:minHeight="?android:attr/listPreferredItemHeightSmall"
    12.     android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
    13.     android:background="@color/logoGreen"
    14.     android:paddingRight="?android:attr/listPreferredItemPaddingRight" />
    15.  
    Although it doesn't give me yet what I am wanting, it is just now a matter of changing this layout_spinner.xml to what I need.
     
Loading...
Similar Threads - Customising spinner
  1. Prashant Sontale
    Replies:
    0
    Views:
    211
  2. Prashant Sontale
    Replies:
    0
    Views:
    213
  3. Prashant Sontale
    Replies:
    2
    Views:
    397
  4. Mingi
    Replies:
    0
    Views:
    201
  5. AbdouMike
    Replies:
    2
    Views:
    1,303
  6. Mingi
    Replies:
    0
    Views:
    1,143
  7. Mingi
    Replies:
    1
    Views:
    1,238
  8. Mingi
    Replies:
    0
    Views:
    645
  9. Mingi
    Replies:
    3
    Views:
    8,436
  10. Dan Davies
    Replies:
    4
    Views:
    618

Share This Page

Loading...