单击列表视图项以启动新活动不起作用

时间:2019-04-05 06:09:52

标签: c# xamarin.android

我是C#中xamarin android应用程序中的新手。我创建了一个页面活动,该活动打算用名为plays的数组填充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");

        }

    }
}

1 个答案:

答案 0 :(得分:1)

根据我的问题是您的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;

 }