[mono-android] Creating a list of TextViews

Jonathan Pryor jonp at xamarin.com
Wed Feb 29 20:43:58 UTC 2012


On Feb 29, 2012, at 7:40 AM, Paul Johnson wrote:
> Can I enumerate the Resource.Id.textDate values, pass them into the FindViewById directly?

You have full System.Reflection, so something like this (untested!) could work:

	var ids = typeof(Resources.Id);

	var tl = new List<TextView>();
	for (int i = 1; i <= 35; ++i) {
		var f = ids.GetField("textDate" + i);
		if (f == null)
			continue;
		int value = (int) f.GetValue (null);
		tl.Add (FindViewById<TextView>(value));
	}

The only downside is that'll require Reflection, and I have no idea what the performance will look like in practice. It might be faster to instead do:

	foreach (int id in new[]{
				Resources.Id.textDate1,
				Resources.Id.textDate2,
				Resources.Id.textDate3,
				/* ... */ }) {
		tl.Add (FindViewById<TextView>(id));
	}

 - Jon



More information about the Monodroid mailing list