当我在xamarin android上创建标题列表视图时,我遇到了一些错误

时间:2016-04-20 01:20:50

标签: android xamarin xamarin.android



  public class CustomAdapter : BaseAdapter{

	private  const int TYPE_ITEM = 0;
	private const int TYPE_SEPARATOR = 1;

	private List<String> mData;
	private TreeSet sectionHeader;

	private LayoutInflater mInflater;

	public CustomAdapter(Context context) {
		mInflater = (LayoutInflater) context
			.GetSystemService(Context.LayoutInflaterService);
	}

	public void addItem( String item) {
		mData.Add(item);
		NotifyDataSetChanged();
	}

	public void addSectionHeaderItem(String item) {
		mData.Add(item);
		sectionHeader.Add(mData.Count - 1);
		NotifyDataSetChanged();
	}

	public int getItemViewType(int position) {
		return sectionHeader.Contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
	}


	public int getViewTypeCount {
		get{ return 2; }
	}


	public override int Count {
		get {return mData.Count;}
	}


	public override GetItem this[int position] {
		get{ return mData [position]; }
	}
		
	public override long GetItemId(int position) {
		return position;
	} 

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		int rowType = getItemViewType(position);

		if (convertView == null) {
			holder = new ViewHolder();
			switch (rowType) {
			case TYPE_ITEM:
				convertView = mInflater.Inflate(Resource.Layout.textViewItemsSeparator, parent);
				holder.textView = (TextView) convertView.FindViewById(Resource.Id.textviewHeaderItems);
				break;
			case TYPE_SEPARATOR:
				convertView = mInflater.Inflate(Resource.Layout.textViewHeaderItems, parent);
				holder.textView = (TextView) convertView.FindViewById(Resource.Id.textviewItemsSeparator);
				break;
			}
			convertView.Tag=holder;
		} else {
			holder = (ViewHolder)convertView.Tag as ViewHolder;
		}
		holder.textView.Text=mData[position];

		return convertView;
	}

	public  class ViewHolder:Java.Lang.Object {
		public TextView textView;
	}
&#13;
&#13;
&#13;

&#13;
&#13;
OnActivityCreated:
mAdapter=new CustomAdapter(this);
			for (int i = 0; i <= items.Count; i++) {
				mAdapter.addItem (items [i].ToString ());
				if (i == 0) {
					mAdapter.addSectionHeaderItem ("Music");
				} else if(i==13) {
					mAdapter.addSectionHeaderItem ("Audio");
				}
			}
			lst.Adapter = new CustomAdapter (Activity, mAdapter);
&#13;
&#13;
&#13;

为ListView创建自定义适配器

您必须覆盖getViewTypeCount()方法。此方法返回将由getView方法创建的视图类型的数量。

我正在尝试创建标题列表视图。

1 个答案:

答案 0 :(得分:1)

使用TextView

的属性设置文本
holder.textView.Text = mData[position];

在幕后,Xamarin将 C# string转换为 Java CharSequence并为您调用setText

<小时/> P.S

您必须查看文档!当你是初学者时,这是必须的。