Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8097927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:54:55+00:00 2026-06-05T21:54:55+00:00

I have an Android application with a Spinner and want to fill it dynamically

  • 0

I have an Android application with a Spinner and want to fill it dynamically with my own objects. The objects do exist already as a List<T>.

The objects are of type Category:

public class Category implements Serializable {
    private Long id;
    private String name;

    // constructors
    // getter & setter
    // hashCode, equals
    // toString
}

I know that I have to write a Adapter. How do I do that? I’ve tried to find some examples… no luck. Please advice.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T21:54:57+00:00Added an answer on June 5, 2026 at 9:54 pm

    Here is my 5 cents. I had a similar problem. I was working with SimpleCursorAdapter which implements interface SpinnerAdapter, but arrived only until SDK version 11 (Android 3.0). I intended my app to work with SDK 8 (Android 2.2) and up, so I had to replace SimpleCursorAdapter with another, or my own. The real challenger was that I also used a custom XML layout for spinner and in it showed several fields from cursor i.e. cursor adapter. So here is my solution after a lot of research, and the info wasn’t easy to come by.

    Here is the layout file used in spinner named spin_layout.xml :

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" >
    <TextView 
        android:id="@+id/field1"
        android:textColor="#000"
        android:gravity="center"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:textSize="24sp" />
    <TextView 
        android:id="@+id/field2"
        android:textColor="#000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:textSize="24sp" />
    </LinearLayout>
    

    And here is the adapter implementing SpinnerAdapter and extending (using as a little helper) BaseAdapter. The Cursor that was used originally was transformed into List and passed in constructor, together with activity containing the spinner.

    public class MyCursorAdapter extends BaseAdapter implements SpinnerAdapter{
        private Activity activity;
        private List<BusLines> list_bsl; 
    
        public MyCursorAdapter(Activity activity, List<BusLines> list_bsl){
            this.activity = activity;
            this.list_bsl = list_bsl;
        }
    
        public int getCount() {
            return list_bsl.size();
        }
    
        public Object getItem(int position) {
            return list_bsl.get(position);
        }
    
        public long getItemId(int position) {
            return list_bsl.get(position).getId();
        }
    
        public View getView(int position, View convertView, ViewGroup parent) {
    
        View spinView;
        if( convertView == null ){
            LayoutInflater inflater = activity.getLayoutInflater();
            spinView = inflater.inflate(R.layout.spin_layout, null);
        } else {
             spinView = convertView;
        }
        TextView t1 = (TextView) spinView.findViewById(R.id.field1);
        TextView t2 = (TextView) spinView.findViewById(R.id.field2);
        t1.setText(String.valueOf(list_bsl.get(position).getLine_Num()));
        t2.setText(list_bsl.get(position).getName());
        return spinView;
        }
    }
    

    Unlike other solutions you find on the web, method getItemId establishes link with id field from database, just like SimpleCursorAdapter. That id is the argument passed in onItemSelected(AdapterView arg0, View arg1, int position, long id) in OnItemSelectedListener for spinner.setOnItemSelectedListener. Method getView inflated spin_layout.xml, identifies the two views contained in layout and assigns them values (as String!).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have PhoneGap-Android application and I want to check network connection type (No connection,
I have an Android application and an in-app purchase. I want to post the
I have an application, which has a Spinner that I want populated with some
I want to add a spinner to my android application that looks like that
I have created a drop down in my android application using a spinner. However
I have an Android application that uses tabs. I already have the tabs, and
I have an Android application which talks to a public Data-API by calling URLs.
I have an android application and want to save the state of instance in
I have an android application where I want to display a LinearLayout when user
In my Android application, I am using spinner, and I have loaded data from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.