我是c#中xamarin android app的新手。我创建了一个页面活动,它打算用名为plays的数组填充listview.it成功地显示了包含数组项的listview。当我点击listview项目时没有任何事情发生。它应该根据项目的位置开始新的活动。我认为listv.itemclick事件处理程序存在问题。 pageActivity如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Net;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Webkit;
using Android.Widget;
namespace smartapp
{
[Activity(Label = "PageActivity")]
public class PageActivity : Activity
{
ListView listv;
ArrayAdapter adapter;
ArrayList Plays;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Pages);
getData();
listv = FindViewById<ListView>(Resource.Id.listv);
adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, Plays);
listv.Adapter = adapter;
listv.FastScrollEnabled = true;
// Create your application here
listv.ItemClick+= (object sender,AdapterView.ItemClickEventArgs e)=>{
Intent myintent;
switch(Plays[e.Position])
{
case 0:
myintent=new Intent(this,typeof(HelpActivity));
StartActivity(myintent);
break;
case 1:
myintent=new Intent(this,typeof(IslamActivity));
StartActivity(myintent);
break;
case 2:
myintent=new Intent(this,typeof(AllPagesActivity));
StartActivity(myintent);
break;
}
};
}
private void getData()
{
Plays = new ArrayList();
Plays.Add("Care Center");
Plays.Add("Real Islam");
Plays.Add("All Pages");
}
}
}
根据我的问题是你的Switch应该是这样的:
switch(e.Position)
{
case 0:
myintent=new Intent(this,typeof(HelpActivity));
StartActivity(myintent);
break;
case 1:
myintent=new Intent(this,typeof(IslamActivity));
StartActivity(myintent);
break;
case 2:
myintent=new Intent(this,typeof(AllPagesActivity));
StartActivity(myintent);
break;
}