如何在具有许多listview的许多活动中使用适配器

时间:2018-10-12 02:54:07

标签: listview xamarin xamarin.android adapter baseadapter

hi iam在许多页面上工作,项目包含许多listview 当我将基本adaper使用它的工作进行一次激活时,但是另一页面却出现错误

这里是我的代码`

命名空间LastTest {     [活动(标签=“ LastTest”,MainLauncher =真)     公共类MainPage:活动     {         公共列表tableItems = new List();

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        NameClass Recitesn = new NameClass();
        tableItems = Recitesn.Namelist();

        ListView listView = FindViewById<ListView>(Resource.Id.listView1);
        listView.Adapter = new HomeScreenAdapter(this, tableItems);
        listView.ItemClick += OnListItemClick;

    }

    void OnListItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
        Context context = Android.App.Application.Context;

        Intent myintent;
        switch (e.Position)
        {
            case 0:
                myintent = new Intent(context, typeof(Activity1));
                StartActivity(myintent);
                break;

        }

    }
}

public class HomeScreenAdapter : BaseAdapter<NameClass>
{
    List<NameClass> items;
    Activity context;

    public HomeScreenAdapter(Activity context, List<NameClass> items)
        : base()
    {
        this.context = context;
        this.items = items;
    }
    public override long GetItemId(int position)
    {
        return position;
    }
    public override NameClass this[int position]
    {
        get { return items[position]; }
    }
    public override int Count
    {
        get { return items.Count; }
    }
    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        var item = items[position];
        View view = convertView;
        if (view == null) // no view to re-use, create new
            view = context.LayoutInflater.Inflate(Resource.Layout.CustomView, null);
        view.FindViewById<TextView>(Resource.Id.Text1).Text = item.Name;
        //view.FindViewById<TextView>(Resource.Id.Text2).Text = item.ServerName;
        // view.FindViewById<ImageView>(Resource.Id.Image).SetImageResource(item.ImageResourceId);
        return view;
    }
}

}`

第二次活动

enter code herenamespace LastTest

{     [Activity(Label =“ Activity1”)]     公共课程Activity1:活动     {           列出tableItems = new List();

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your application here
        SetContentView(Resource.Layout.Main);
         JobsClass job = new JobsClass();
         tableItems = job.Joblist();

        ListView listView = FindViewById<ListView>(Resource.Id.listView1);
        listView.Adapter = new HomeScreenAdapter(this, tableItems);

    }
}

}

此处错误

            listView.Adapter = new HomeScreenAdapter(this, tableItems);

我复制我的错误

enter code here error CS1503: Argument 2: cannot convert from 'System.Collections.Generic.List<LastTest.JobsClass>' to 'System.Collections.Generic.List<LastTest.NameClass>'

我的课1

命名空间LastTest {     公共类NameClass     {         公共字符串名称;

    public NameClass() { }
    public NameClass(string Name)
    {
            this.Name=Name;
    }

    public List<NameClass> Namelist()
    {
        List<NameClass> NamelistInfo = new List<NameClass>();

        NamelistInfo.Add(new NameClass("david"));
        NamelistInfo.Add(new NameClass("roben"));
        NamelistInfo.Add(new NameClass("jouj"));
        NamelistInfo.Add(new NameClass("caty"));
        NamelistInfo.Add(new NameClass("sale"));

        return (NamelistInfo);

    }
}

}

我的课2

namespace LastTest

{     职业JobsClass     {         公共字符串作业;

    public JobsClass() { }
    public JobsClass(string Job)
    {
        this.Job = Job;
    }

    public List<JobsClass> Joblist()
    {
        List<JobsClass> JoblistInfo = new List<JobsClass>();

        JoblistInfo.Add(new JobsClass("A"));
        JoblistInfo.Add(new JobsClass("B"));
        JoblistInfo.Add(new JobsClass("C"));
        JoblistInfo.Add(new JobsClass("D"));
        JoblistInfo.Add(new JobsClass("E"));

        return (JoblistInfo);

    }
}

}

2 个答案:

答案 0 :(得分:0)

错误消息是自我解释:

您无法使用JobsClass创建HomeScreenAdapter的实例,因为JobsClass不是NameClass:

HomeScreenAdapter : BaseAdapter<NameClass>

答案 1 :(得分:0)

您已经创建了NameClass类型的适配器。在第二个活动中,您传递的是JobClass类型的“ tableItems”。

您还必须为JobClass类型创建一个新的适配器,或者始终传递NameClass类型的参数。