对话框Xamarin中的微调器

时间:2017-03-10 10:44:35

标签: c# xamarin.android spinner

我正在开发一个Android项目,我必须在对话框中显示带有textview和按钮(主要布局)的微调器。单击注册按钮(注册布局)对话框出现但问题是旋转器是空白而不显示我想要显示的列表。 我在最近两天的工作,我是Xamarine和c#的初学者。

请建议我如何以及在何处设置Spinner的列表方法

我想我错过了什么。感谢任何帮助。

这是我的主要活动:

[Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
    private Spinner sp;
    private CustomSpinner adapter;
    private JavaList<CountryName> countries;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Register);


        //passing array in spinner
        sp = FindViewById<Spinner>(Resource.Id.country);
        countries = CountriesList.GetCountrys();
        adapter = new CustomSpinner(this, countries);
        sp.Adapter = adapter;
        sp.ItemSelected += sp_ItemSelected;

        Button Reg = FindViewById<Button>(Resource.Id.button1);
        Reg.Click += delegate
         {

             var alert = new AlertDialog.Builder(this);
             alert.SetView(LayoutInflater.Inflate(Resource.Layout.Main, null));
             alert.Create().Show();

         };
    }


    void sp_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
    {
        Toast.MakeText(this, countries[e.Position].Name, ToastLength.Short).Show();
    }
}

自定义微调器:

namespace App1.Assets.Ccode.Cspinner
{

class CustomSpinner : BaseAdapter
{
    private Context c;
    private JavaList<CountryName> countries;
    private LayoutInflater inflater;

    public CustomSpinner(Context c, JavaList<CountryName> countries)
    {
        this.c = c;
        this.countries = countries;
    }

    public override Object GetItem(int position)
    {
        return countries.Get(position);
    }

    public override long GetItemId(int position)
    {
        return position;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        if (inflater == null)
        {
            inflater =     (LayoutInflater)c.GetSystemService(Context.LayoutInflaterService);
        }


        if (convertView == null)
        {
            convertView = inflater.Inflate(Resource.Layout.country_list, parent, false);
        }



        TextView nameTxt = convertView.FindViewById<TextView>(Resource.Id.nameTxt);
        ImageView img = convertView.FindViewById<ImageView>(Resource.Id.India);

        //BIND
        nameTxt.Text = countries[position].Name;
        img.SetImageResource(countries[position].Image);


        return convertView;
    }

    public override int Count
    {
        get
        {
            return countries.Size();
        }

  }

  }
  }

Main.axml

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <Spinner
    android:layout_width="99.0dp"
    android:layout_height="36.5dp"
    android:id="@+id/country"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="25dp"
    android:layout_marginRight="40dp" />
<EditText
    android:layout_width="308.0dp"
    android:layout_height="wrap_content"
    android:id="@+id/phoneNumber"
    android:hint="Phone Number"
    android:inputType="phone"
    android:layout_marginTop="25dp"
    android:layout_marginLeft="140dp" />
<Button
    android:layout_width="match_parent"
    android:layout_height="61.0dp"
    android:text="SignUp"
    android:id="@+id/loginButton"
    android:layout_marginLeft="40dp"
    android:layout_marginRight="40dp"
    android:layout_marginTop="120dp" />
 </RelativeLayout>

注册布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<Button
    android:text="Register"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/button1"
    android:layout_marginTop="80dp" />
</LinearLayout>

0 个答案:

没有答案