Question

Hello I'm new at Android. I want to populate a Spinner with a list of objects. I have googled how to do it but I just find examples with an array of strings.

Can any one help me?

This is my code:

Categories class:

public class Categories 
{
    @com.google.gson.annotations.SerializedName("id")
    private String mId;

    @com.google.gson.annotations.SerializedName("name")
    private String mName;

    public Categories()
    {}

    public Categories(String id, String name)
    {
        this.setId(id);
        this.setName(name);
    }

    @Override
    public String toString() 
    {
        return mName;
    }

//  ********    GET     *************
    public String getId()
    {
        return mId;
    }

    public String getName()
    {
        return mName;
    }

//  ********    SET     *************

    public final void setId(String id)
    {
        mId = id;
    }

    public final void setName(String name)
    {
        mName = name;
    }
}

This is my Activity code:

public class AgregarActividadActivity extends ActionBarActivity 
{

    private MobileServiceClient mClient;
    private MobileServiceTable<Activities> mActivitiesTable;
    private MobileServiceTable<Categories> mCategoriesTable;
    private MobileServiceTable<Projects> mProjectsTable;


    private EditText mTxtTitulo;
    private EditText mTxtDescr;
    String categryId = null;
    List<Categories> catList = new ArrayList<Categories>();

    Spinner spEstado;



    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_agregar_actividad);

        try 
        {
            mClient = new MobileServiceClient(
                    "https://site.azure-mobile.net/",
                    "AppKey", 
                    this);
            mActivitiesTable = mClient.getTable(Activities.class);
            mCategoriesTable = mClient.getTable(Categories.class);
        } 
        catch (MalformedURLException e) 
        {
            createAndShowDialogExc(new Exception("There was an error creating the Mobile Service. Verify the URL"), "Error");
        }

        mTxtTitulo = (EditText) findViewById(R.id.txtTitulo);
        mTxtDescr = (EditText) findViewById(R.id.txtDescripcion);
        getCategories();

        spEstado = (Spinner)this.findViewById(R.id.spEstado);

        ArrayAdapter<Categories> Adapter = new ArrayAdapter<Categories>(this,
                android.R.layout.simple_spinner_item, catList);
        Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spEstado.setAdapter(Adapter);
        spEstado.setOnItemSelectedListener(
                new AdapterView.OnItemSelectedListener() {
                    public void onItemSelected(
                            AdapterView<?> parent, 
                            View view, 
                            int position, 
                            long id) {
                        Categories item = (Categories) parent.getItemAtPosition(position);
                    }

                    public void onNothingSelected(AdapterView<?> parent) {
                    }
                }
            );

        spProjects = (Spinner)this.findViewById(R.id.spProyecto);
        ArrayAdapter<Projects> proAdapter = new ArrayAdapter<Projects>(this,
                android.R.layout.simple_spinner_item, proList);
        proAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spProjects.setAdapter(proAdapter);

    }

    private void getCategories()
    {
        mCategoriesTable.execute(new TableQueryCallback<Categories>() 
        {
            public void onCompleted(List<Categories> result, int count, Exception exception, ServiceFilterResponse response) 
            {
                if (exception == null) 
                {
                    for (Categories item : result) 
                    {
                        catList.add(item);
                    }
                } 
                else 
                {
                    createAndShowDialog(exception, "Error");
                }
            }
        });
    }
}

I get the dropdownlist with the objects, but when I select one item, it is not displayed as the selected item, when the dropdownlist is hidden.

Any idea will help me! Thank you!!

Was it helpful?

Solution

You need to write a CustomAdapter for this. It is similar to writing a CustomAdapter for a ListView. You can look at Custom Adapter for List View for an idea

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top