问题描述
我有两个适配器,我想将2个适配器合并为一个适配器xamarin android
i have two adapters and i want to merging the 2 adapters into a single adapter xamarin android
第一个适配器:
private ArrayAdapter _adapter;
第二个适配器:
class HomeScreenAdapterEmp : BaseAdapter<employer>
{
List<employer> items;
Activity context;
int a;
public HomeScreenAdapterEmp(Activity context, List<employer> items)
: base()
{
this.context = context;
this.items = items;
//this.A = c;
}
我想将2个适配器合并为一个适配器
i want to merging the 2 adapters into a single adapter
_adapter = new ArrayAdapter(this,
Android.Resource.Layout.SimpleListItem1, TableEmp);//2
listView.Adapter = _adapter;//2
listView.Adapter = new HomeScreenAdapterEmp(this, TableEmp);// 1
listView.ItemClick += ListView_ItemClick;//1
sv.QueryTextChange += Sv_QueryTextChange;
listView.ItemClick += ListView_ItemClick1;
推荐答案
按照Yupi的建议,您可以创建您的 MergeAdapter
,但是在您的代码中,我发现您的 ArrayAdapter
和 HomeScreenAdapterEmp
具有相同的数据资源 TableEmp
?根据您的代码, List< employer>项目;
,此 TableEmp
的类型应为 List< employer>
.这是错字吗?
As Yupi suggested, you can create your MergeAdapter
, but in your code I find that your ArrayAdapter
and HomeScreenAdapterEmp
have the same data resource TableEmp
? Based on your code List<employer> items;
, this TableEmp
should be the type List<employer>
. Is this a typo?
无论如何,您可以像下面这样创建您的 MergeAdapter
:
Anyway, you can create your MergeAdapter
for example like this:
public class MergeAdapter : BaseAdapter
{
protected List<IListAdapter> pieces = new List<IListAdapter>();
protected string noItemsText;
public MergeAdapter()
{
}
public void AddAdapter(IListAdapter adapter)
{
pieces.Add(adapter);
adapter.RegisterDataSetObserver(new CascadeDataSetObserver());
}
private class CascadeDataSetObserver : DataSetObserver
{
public override void OnChanged()
{
base.OnChanged();
}
public override void OnInvalidated()
{
base.OnInvalidated();
}
}
public override int Count
{
get
{
int total = 0;
foreach (var piece in pieces)
{
total += piece.Count;
}
if (total == 0 && noItemsText != null)
{
total = 1;
}
return total;
}
}
public override Java.Lang.Object GetItem(int position)
{
foreach (var piece in pieces)
{
int size = piece.Count;
if (position < size)
return (piece.GetItem(position));
position -= size;
}
return null;
}
public override int ViewTypeCount
{
get
{
int total = 0;
foreach (var piece in pieces)
{
total += piece.ViewTypeCount;
}
return (Java.Lang.Math.Max(total, 1));
}
}
public override int GetItemViewType(int position)
{
int typeOffset = 0;
int result = -1;
foreach (var piece in pieces)
{
int size = piece.Count;
if (position < size)
{
result = typeOffset + piece.GetItemViewType(position);
break;
}
position -= size;
typeOffset += piece.ViewTypeCount;
}
return result;
}
public override long GetItemId(int position)
{
foreach (var piece in pieces)
{
int size = piece.Count;
if (position < size)
{
return (piece.GetItemId(position));
}
position -= size;
}
return (-1);
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
foreach (var piece in pieces)
{
int size = piece.Count;
if (position < size)
return piece.GetView(position, convertView, parent);
position -= size;
}
if (noItemsText != null)
{
TextView text = new TextView(parent.Context);
text.Text = noItemsText;
return text;
}
return null;
}
public void SetNoItemText(string text)
{
noItemsText = text;
}
public IListAdapter GetAdapter(int position)
{
foreach (var piece in pieces)
{
int size = piece.Count;
if (position < size)
return piece;
position -= size;
}
return null;
}
}
并像这样使用它:
ListView lv = FindViewById<ListView>(Resource.Id.lv);
for (int i = 0; i < 100; i++)
{
TableEmp.Add(new employer() { ID = i, Name = "Name " + i });
_stringEmp.Add("Item " + i);
}
var _adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, _stringEmp);
var homeadapter = new HomeScreenAdapterEmp(this, TableEmp);
var mergeadapter = new MergeAdapter();
mergeadapter.AddAdapter(_adapter);
mergeadapter.AddAdapter(homeadapter);
mergeadapter.SetNoItemText("No Item");
lv.Adapter = mergeadapter;
这篇关于我如何将两个适配器添加到一个 listView (xamarin android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!