我在Android应用程序中使用expandablelistadapter。实际上根据我的要求,我正在显示一个组标题,该组的每个孩子都包含3个文本视图。 但是我无法运行我的应用程序,因为我在GetChild,GetChildView和GetGroup,GetGroupView附近遇到错误。
错误信息如下(我粗体提到)
我尝试显式转换类型,但它不起作用。在这方面请帮助我,因为我的工作因此而被推迟。
public override Java.Lang.Object GetGroup(int groupPosition)
{
return groups[groupPosition]; - > Cannot implicitly convert type DiningGroup to Java.Lang.Object
}
public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
{
List<DiningChild> chList = groups[groupPosition].Items;
return chList[childPosition]; - > Cannot implicitly convert type DiningChild to Java.Lang.Object
}
public override View GetChildView(int groupPosition, int childPosition,
bool isLastChild, View convertView, ViewGroup parent)
{
DiningChild child = (DiningChild)GetChild(groupPosition, childPosition); - > cannot convert type Java.lang.Object to DiningChild
if (convertView == null)
{
convertView = _activity.LayoutInflater.Inflate(Resource.Layout.Dining_ListControl_ChildItem, null);
}
var tvfooditem = convertView.FindViewById<TextView>(Resource.Id.txtfooditem);
var tvfooddesc = convertView.FindViewById<TextView>(Resource.Id.txtfooddesc);
var tvfoodprice = convertView.FindViewById<TextView>(Resource.Id.txtfoodprice);
tvfooditem.SetText(child.FoodItemName, TextView.BufferType.Normal);
tvfooddesc.SetText(child.FoodDescription, TextView.BufferType.Normal);
tvfoodprice.SetText(child.FoodPrice, TextView.BufferType.Normal);
return convertView;
}
public override View GetGroupView(int groupPosition, bool isExpanded,
View convertView, ViewGroup parent)
{
DiningGroup group = (DiningGroup)GetGroup(groupPosition); - > cannot convert type Java.lang.Object to DiningGroup
if (convertView == null)
{
convertView = _activity.LayoutInflater.Inflate(Resource.Layout.Dining_ListControl_Group, null);
}
var tvcategoryname = convertView.FindViewById<TextView>(Resource.Id.txtCategory);
tvcategoryname.SetText(group.Name, TextView.BufferType.Normal);
return convertView;
}
答案 0 :(得分:0)
如注释中所述,对于返回Java.Lang.Object的方法,抛出NotImplementedException。添加您自己的方法以使用指定的参数获取DiningChild。
public override Java.Lang.Object GetGroup(int groupPosition)
{
throw new NotImplementedException();
}
public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
{
throw new NotImplementedException();
}
protected GetDiningChild GetDiningChild(int groupPosition, int childPosition)
{
List<DiningChild> chList = groups[groupPosition].Items;
return chList[childPosition];
}
protected GetDiningGroup GetDiningGroup(int groupPosition)
{
return groups[groupPosition];
}
public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
{
DiningChild child = GetDiningChild(groupPosition, childPosition);
if (convertView == null)
{
convertView = _activity.LayoutInflater.Inflate(Resource.Layout.Dining_ListControl_ChildItem, null);
}
var tvfooditem = convertView.FindViewById<TextView>(Resource.Id.txtfooditem);
var tvfooddesc = convertView.FindViewById<TextView>(Resource.Id.txtfooddesc);
var tvfoodprice = convertView.FindViewById<TextView>(Resource.Id.txtfoodprice);
tvfooditem.SetText(child.FoodItemName, TextView.BufferType.Normal);
tvfooddesc.SetText(child.FoodDescription, TextView.BufferType.Normal);
tvfoodprice.SetText(child.FoodPrice, TextView.BufferType.Normal);
return convertView;
}
public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
{
DiningGroup group = GetDiningGroup(groupPosition);
if (convertView == null)
{
convertView = _activity.LayoutInflater.Inflate(Resource.Layout.Dining_ListControl_Group, null);
}
var tvcategoryname = convertView.FindViewById<TextView>(Resource.Id.txtCategory);
tvcategoryname.SetText(group.Name, TextView.BufferType.Normal);
return convertView;
}