[mono-android] v.FindViewById<Button>(Resource.Id.btnRemove) return null

Jonathan Pryor jpryor at novell.com
Sun Mar 20 11:00:18 EDT 2011


Note: what follows is conjecture; please test. :-)

On Mar 20, 2011, at 4:35 AM, Asmaa Mohamed Roushdy wrote:
> I created custom datagrid activity as follow
>  
> [Activity(Label = "GridView", MainLauncher = true)]
>     public class MyGridView : Activity
>     {
>         List<Quote> books = new List<Quote>();

The probable problem is that `books` is a List<T>. You should use Android.Runtime.JavaList<T>:

	http://lists.ximian.com/mailman/private/monodroid/2011-March/003706.html
	http://lists.ximian.com/mailman/private/monodroid/2011-March/003748.html

The reason is because most collections, when passed to Android code, are copied. Consequently, when you later call books.Add(), the Java code will _not_ see those changes:

	http://mono-android.net/Documentation/API_Design#Collections

>         void textView_ItemClick(object sender, ItemEventArgs e)
>         {
>             books.Add(new Quote(((TextView)e.View).Text, 10, 10));
>             mcqListAdapter.NotifyDataSetChanged();

Like here. :-)

Thus, mcqListAdapter won't "see" the Quote you just added to `books` (because when you created the LitemItemAdapter the contents of `books` was copied into a Java-side collection).

Thus, here:

>     class LitemItemAdapter : BaseAdapter, Android.Views.View.IOnClickListener
>     {
... 
> 
>         public override View GetView(int position, View convertView, ViewGroup parent)
>         {
>             View v = convertView;
>             if (v == null)
>             {
>                 LayoutInflater vi = (LayoutInflater)mContext.GetSystemService(Context.LayoutInflaterService);
>                 v = vi.Inflate(Resource.Layout.RawLayout, null);
>             }
>             Quote item = books[position];

`item` will be non-null (as a value was added in textView_ItemClick(), above), but the LitemItemAdapter can't know about that item, which I assume is causing the v.FindViewById<T>() call to fail.

 - Jon



More information about the Monodroid mailing list