0

i'using this code for adding items from sql into a button with spinner style:

This is my xml file

<Button
    android:text="Button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/btn_dropdown"
    android:id="@+id/button1" />

And this is my code:

  Button spinner1 = FindViewById<Button>(Resource.Id.button1);
        var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem);
         spinner1.Adapter = adapter;

        con.Open();

        SqlCommand sqlCmd = new SqlCommand("Select name from customers)", con);

        SqlDataReader sqlReader = sqlCmd.ExecuteReader();
        while (sqlReader.Read())
        {
            adapter.Add((string)sqlReader["name "]);
        }
        sqlReader.Close();
        con.Close();

I get error Button does not contain definition for adapater and no extension method adapter....How can i fix this? Also i dont want to use spinner cause i cant set a title on it

DiH
  • 451
  • 2
  • 8
  • 18
  • You are casting a `Button` to be a `Spinner`? Use a `Spinner` control instead. See the docs for more information: https://developer.android.com/guide/topics/ui/controls/spinner.html – Jon Douglas Aug 12 '16 at 21:27
  • I dont want to use spinner cause i cant set a title to spinner. Like "select one from below...".Also If i will add an item for a title in spinner i cant remove the title in dropdown. – DiH Aug 12 '16 at 21:30
  • `Button` does not have a method to set an `Adapter`. You need to use `Spinner` or create your own control to do this. https://developer.android.com/reference/android/widget/Spinner.html#setAdapter(android.widget.SpinnerAdapter) – Jon Douglas Aug 12 '16 at 21:31
  • I found in forums that the easiest way to adding title in a spinner is to use a button with spinner style.http://stackoverflow.com/questions/38920007/how-to-hide-first-item-in-an-android-spinner-dropdown?noredirect=1#comment65197133_38920007 – DiH Aug 12 '16 at 21:33
  • You need to adhere to what the APIs expect. The error at hand is saying that the `Button` class does not have these definitions. The solution that you are referring to is to use a `Button` as a mechanism to fire an `AlertDialog` that shows the current selection. Most of these other solutions use an `Adapter` in the `Spinner` class. http://stackoverflow.com/a/9086249/1048571 – Jon Douglas Aug 12 '16 at 21:42
  • Solved I found what i was need in xamarin forums:https://forums.xamarin.com/discussion/60417/how-to-hide-first-item-in-an-android-spinner Do you believe that should i delete this question? – DiH Aug 12 '16 at 21:54

0 Answers0