C# 对话框中的微调器Xamarin
我正在从事一个Android项目,在该项目中,我必须在对话框中显示带有文本视图和按钮(主布局)的微调器。单击“注册”按钮(注册布局)时,对话框出现,但问题是微调器为空,没有显示我要显示的列表。 我从前两天开始做这个,我是Xamarine和c#的初学者 请建议我如何以及在哪里设置微调器列表方法 我想我遗漏了什么。谢谢你的帮助 以下是我的主要活动:C# 对话框中的微调器Xamarin,c#,xamarin.android,spinner,C#,Xamarin.android,Spinner,我正在从事一个Android项目,在该项目中,我必须在对话框中显示带有文本视图和按钮(主布局)的微调器。单击“注册”按钮(注册布局)时,对话框出现,但问题是微调器为空,没有显示我要显示的列表。 我从前两天开始做这个,我是Xamarine和c#的初学者 请建议我如何以及在哪里设置微调器列表方法 我想我遗漏了什么。谢谢你的帮助 以下是我的主要活动: [Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")] pub
[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();
}
}
[活动(Label=“App1”,MainLauncher=true,Icon=“@drawable/Icon”)]
公共课活动:活动
{
私人纺纱机;
专用微调器适配器;
私营国家;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Register);
//旋转器中的传递数组
sp=FindViewById(Resource.Id.country);
countries=CountriesList.getcountries();
适配器=新的CustomSpinner(此,国家/地区);
sp.适配器=适配器;
sp.ItemSelected+=sp_ItemSelected;
Button Reg=FindViewById(Resource.Id.button1);
注册点击+=委托
{
var alert=new AlertDialog.Builder(此);
alert.SetView(LayoutInflater.Inflate(Resource.Layout.Main,null));
alert.Create().Show();
};
}
void sp_ItemSelected(对象发送方,AdapterView.ItemSelectedEventArgs e)
{
Toast.MakeText(这是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();
}
}
}
}
名称空间App1.Assets.Ccode.Cspinner
{
类CustomSpinner:BaseAdapter
{
私有上下文c;
私营国家;
私人充气机;
公共CustomSpinner(上下文c、JavaList国家/地区)
{
这个.c=c;
这个国家=国家;
}
公共覆盖对象GetItem(int位置)
{
返回国家。获取(位置);
}
公共覆盖长GetItemId(int位置)
{
返回位置;
}
公共覆盖视图GetView(int位置、视图转换视图、视图组父视图)
{
如果(充气器==null)
{
充气器=(LayoutInflater)c.GetSystemService(Context.LayoutInflaterService);
}
if(convertView==null)
{
convertView=充气机。充气(Resource.Layout.country\u列表,父项,false);
}
TextView nameTxt=convertView.FindViewById(Resource.Id.nameTxt);
ImageView img=convertView.findviewbyd(Resource.Id.India);
//束缚
nameTxt.Text=国家[职位].Name;
img.SetImageResource(国家[位置].Image);
返回视图;
}
公共覆盖整数计数
{
得到
{
返回国。大小();
}
}
}
}
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>
能否显示CustomSpinner
类的代码?适配器将负责显示数据。@apineda请查看我编辑的问题。谢谢。