1

Let's say I have a Spinner view with 10 Countries (String). I want to use this spinner in multiple activities, fragments, alertdialogs etc.

How can I do it efficiently and save code lines? Which is the best way?

What I first thought is to create a class that creates the spinner and extends the spinner widget class. Then create objects etc. However, as a begginer, it looks a bit complicated to me, is there an easier way?

EDIT: The spinner is created dynamically ONLY. It has 10 items by default but is gradually populated according to database entries.

ElaGorilaki
  • 227
  • 6
  • 20
  • Sorry, I missed to mention this. The spinner is added programmatically with a few standard values, and then it is populated accordingly to the database entries. – ElaGorilaki Feb 15 '14 at 15:53
  • Try to extend it. It helps you on future projects. Do not try to find a workaround. – A.S. Feb 15 '14 at 15:54
  • You can add the standard values with xml also, look at [this question](http://stackoverflow.com/questions/4029261/populating-spinner-directly-in-the-layout-xml). – takendarkk Feb 15 '14 at 15:54
  • @A.S Do you think this is the most efficient and right way in my case? – ElaGorilaki Feb 15 '14 at 16:00

3 Answers3

1

You can create a separate layout file for this and only have spinner in this.

Wherever you require this you can include in your layouts like

<include layout="@layout/spinnerLayout" />
Ajay S
  • 48,003
  • 27
  • 91
  • 111
1

Create a public static method in any class (but probably one that is intuitive to find*) that populates the spinner. It can take context, database, etc. as inputs, as well as the Spinner itself. Then you can call this same method from any fragment or activity and always get the same thing. Just create your layout (such as with setContentView), get a reference to the spinner from the layout, and pass it to your populater method.

Example:

//in Activity
public void onCreate(Bundle bundle){
    super.onCreate(bundle);

    DatabaseHelper myDataBaseHelper = ...;//

    setContentView(R.layout.my_layout);
    Spinner spinner = (Spinner)findViewById(R.id.my_spinner);
    Util.populateStandardSpinner(myDataBaseHelper, spinner, 
        getApplicationContext());

    //...
}


//Another class
public class Util{
    public static void populateStandardSpinner(DatabaseHelper dbHelper, 
            Spinner spinner, Context context) {
        //Get cursor from dbHelper
        //Create adapter for cursor data and apply it to spinner
}

I suppose you could also extend the Spinner class, but my preference is to avoid coding the data directly into the widget. That would break the model-view-controller design pattern.


*I sometimes just create a class called Util where I put convenient static methods like this. Or if you have a database helper class, that might be an intuitive place to put it.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • This seems to be the answer I was looking for. Let me try it and come back later to accept the answer. Thanks for the detailed reply. The "Util" class seems quite handy for these cases. – ElaGorilaki Feb 15 '14 at 16:32
  • Yes, definitely. I think if there is ever more than three lines of code that you find yourself copying and pasting, you need a method instead to keep the code maintainable. Normally, getting a cursor should be done asynchronously. You can still do that with this method. One way is to create the AsyncTask subclass from within this method with an anonymous class, and use final variables to pass into it. – Tenfour04 Feb 15 '14 at 16:40
  • This is the correct choice and it works like a charm. Easier and handy than extending the spinner class. However I still have some problems, as I want to show the spinner after user's click on an item inside the alert dialog. But that's another issue. Thanks! – ElaGorilaki Feb 17 '14 at 15:09
0

Extend Spinner class itself.

You can initialize adapter inside constructor, dynamically load database entries using AsyncTask, and manage resource usage with onAttachedToWindow and onDetachedFromWindow.

I usually do this when face similar problems, it is convenient, easy to develop and use.

weaknespase
  • 1,014
  • 8
  • 15