,可以通过以下步骤完成:
下面是一个示例代码:
// 创建自定义适配器类
public class CustomAdapter : BaseAdapter<string>
{
private List<string> dataList;
private Context context;
public CustomAdapter(Context context, List<string> dataList)
{
this.context = context;
this.dataList = dataList;
}
public override string this[int position] => dataList[position];
public override int Count => dataList.Count;
public override long GetItemId(int position) => position;
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
{
LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
view = inflater.Inflate(Resource.Layout.list_item_layout, null);
}
TextView textView = view.FindViewById<TextView>(Resource.Id.text_view);
textView.Text = dataList[position];
return view;
}
}
// 在Activity或Fragment中使用ListView和适配器
public class MainActivity : Activity
{
private ListView listView;
private CustomAdapter adapter;
private List<string> dataList;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
listView = FindViewById<ListView>(Resource.Id.list_view);
dataList = new List<string> { "Item 1", "Item 2", "Item 3" };
adapter = new CustomAdapter(this, dataList);
listView.Adapter = adapter;
}
}
在上述示例中,CustomAdapter是自定义的适配器类,继承自BaseAdapter。GetView方法中使用了布局文件list_item_layout来定义列表项的外观,其中包含一个TextView用于显示数据。MainActivity中使用了ListView控件,并将适配器与ListView关联起来。
这样,就可以将列表添加到Xamarin.Android中的ListView中了。
腾讯云相关产品和产品介绍链接地址:
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云