• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Customising a spinner

ShamusVW

Newbie
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:
private Spinner spinner;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_training_layout, container, false);
    spinner = (Spinner) view.findViewById(R.id.spinnerTrainers);
…

This is my class to extract the data, and how I am populating the Spinner:

Code:
   private class ConnectMySql extends AsyncTask<String, Void, String> {
        List<String> trainers = new ArrayList<String>();
        ArrayAdapter<String> adapter;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection(url, user, pass);

                Statement st = con.createStatement();
                ResultSet rs = st.executeQuery("SELECT * FROM pdc_production.die_detail");
                ResultSetMetaData rsmd = rs.getMetaData();

                while (rs.next()) {
                    trainers.add(rs.getString(1).toString() + ":" + rs.getString(2).toString());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "";
        }

        @Override
        protected void onPostExecute(String result) {
            adapter = new ArrayAdapter<String> (getContext(), android.R.layout.simple_spinner_dropdown_item, trainers);
//            adapter = ArrayAdapter.createFromResource(getContext(), trainers, android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);
        }

    }

This line populates the Spinner
Code:
adapter = new ArrayAdapter<String> (getContext(), android.R.layout.simple_spinner_dropdown_item, trainers)
The line right beneath it (commented out)
Code:
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
 
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:
List<String> trainers = new ArrayList<String>();
changed to...
Code:
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:
private class ConnectMySql extends AsyncTask<String, Void, String> {
...
@Override
protected void onPostExecute(String result) {
    adapter = ArrayAdapter.createFromResource(getContext(), R.Array.trainers, R.layout.spinner_layout);
    spinner.setAdapter(adapter);
}
}
 
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:
@Override
protected void onPostExecute(String result) {
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),R.layout.spinner_layout, trainers);
    spinner.setAdapter(adapter);
}

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:
adapter = new ArrayAdapter<String> (getContext(), android.R.layout.simple_spinner_dropdown_item, trainers);

The layout_spinner.xml file:

Code:
<?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
    android:background="@color/logoGreen"
    android:paddingRight="?android:attr/listPreferredItemPaddingRight" />

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.
 
Back
Top Bottom