所以我对Android的开发非常新,我决定和Xamarin一起去,因为我喜欢C#而且我先学会了它。
我的实际问题浮出水面2天前,我有这个应用程序,我创建了几个片段,从点击事件监听器显示,这是一个非常基本的应用程序。我创建了一个List<>我想用于此ListFragment中的项目。我创建了自定义适配器,就像我之前创建的任何其他自定义适配器一样:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using MSApp.Droid.ListClasses;
namespace MSApp.Droid
{
public class CountryAdapter : BaseAdapter
{
public List<Country> countryList { get; set; }
Context context;
public CountryAdapter(Context context)
{
this.context = context;
countryList = new List<Country>();
}
public override int Count
{
get
{
return countryList.Count;
}
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View row = null;
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
row = inflater.Inflate(MSApp.Droid.Resource.Layout.CountryRow, parent, false);
}
else
{
row = convertView;
}
var countryLabel = row.FindViewById<TextView>(MSApp.Droid.Resource.Id.countryTextView);
var countryImage = row.FindViewById<ImageView>(MSApp.Droid.Resource.Id.flagImageView);
var data = countryList[position];
countryLabel.Text = data.name;
countryImage.SetImageResource(data.imageID);
return row;
}
public override Java.Lang.Object GetItem(int position)
{
return null;
}
public override long GetItemId(int position)
{
return countryList[position].id;
}
}
}
但是当我尝试创建ListFragment时,我似乎无法让它一起工作。我有这样的事情:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using MSApp.Droid.ListClasses;
namespace MSApp.Droid.Fragments
{
public class CountryFragment : Android.Support.V4.App.ListFragment
{
private List<Country> countryList { get; set; }
private CountryAdapter countryAdapter { get; set; }
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
countryList = new List<Country>();
countryList.Add(new Country() { name = "Peru", imageID = MSApp.Droid.Resource.Drawable.Peru });
countryAdapter = new CountryAdapter(this);
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
// return inflater.Inflate(Resource.Layout.YourFragment, container, false);
return base.OnCreateView(inflater, container, savedInstanceState);
}
这就是我遇到问题的地方,我无法将上下文设置为它所说的片段,我不能继续前进,我一直在互联网上寻找,youtube,这里,在线课程,但似乎没有什么在任何地方使用这种东西。如果有人可以帮助我指出我正确的方向,那将非常感激。
提前谢谢!