14

I need something like a combobox in access in android, i want to choose the customer per name, but in the background the id should be chosen. how to do?

Mark
  • 369
  • 3
  • 5
  • 11
  • I had same problem and finally found it. Look at this link [Get selected value in spinner such as c# combobox][1] [1]: http://stackoverflow.com/questions/14354782/how-can-i-get-selected-value-in-spinner-such-as-c-sharp-combobox – Hadi.A Jan 19 '13 at 08:20
  • try this example: http://stackoverflow.com/a/17650125/2027232 – string.Empty Mar 04 '14 at 07:01

2 Answers2

26

In android comboboxes are called spinner. Nevertheless, gnugu has posted in his blog his own implementation of a combobox. http://www.gnugu.com/node/57

A simple example of an spinner would be the following. First, edit your XML code with something like this

Spinner android:id="@+id/Spinner01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

Your java code should include something like this, the options are very intuitive. If you are using eclipse it will suggest you some options

public class SpinnerExample extends Activity {
    private String array_spinner[];
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // Here come all the options that you wish to show depending on the
        // size of the array.
        array_spinner=new String[5];
        array_spinner[0]="option 1";
        array_spinner[1]="option 2";
        array_spinner[2]="option 3";
        array_spinner[3]="option 4";
        array_spinner[4]="option 5";
        Spinner s = (Spinner) findViewById(R.id.Spinner01);
        ArrayAdapter adapter = new ArrayAdapter(this,
        android.R.layout.simple_spinner_item, array_spinner);
        s.setAdapter(adapter);
    }
}
glenneroo
  • 1,908
  • 5
  • 30
  • 49
dLobatog
  • 1,751
  • 16
  • 17
  • Thanks, ive got that working BUT the problem is i need the id not the value which is shown. For e.g. Customers: ID Name 123 Robert M. 133 Luisa K. I chose Luisa K. but want then 133 not the Luisa K. – Mark Mar 14 '11 at 08:47
  • What about setting an if statement that if "Robert" has been picked, 123 is set to a variable in the background? – dLobatog Mar 14 '11 at 13:01
  • 2
    How about you make a list of values based off the selected id? One list of strings with names in it, and another list of int's with their id stored in their. Link the spinner to the list of names. Then when you select an item in the spinner, look up the selected id in the list of integers to get the id of the person selected. – Shaun Nov 06 '11 at 20:22
  • how can I change the text color? – Tom Lenc Jul 25 '15 at 13:34
2

An alternate solution to the need to link Customer ID to the selected Item.

To have a simple selector with text you cause make use of the array resources Setup the Spinner in XML

<Spinner android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/colors"/>

If you need more data linked with the spinner you can use Objects to populate the spinner. The default functionality of an ArrayAdapter is to call toString() on any object and pass that to the view.

if (item instanceof CharSequence) {
    text.setText((CharSequence)item);
} else {
    text.setText(item.toString());
}

You can implement toString() in your object and it will display correctly in the spinner. Then to get the data back from the array you can add a handler onto ItemSelected and get the object back from the seed array or the ArrayAdapter.

ArrayAdapter adapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, arrayOfObjects);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
    {
        Log.d(arrayOfObjects[position]._id);
    }

});
Steven
  • 3,200
  • 1
  • 17
  • 16