[mono-android] BaseAdapter and JavaList problem

John Rayner anyerr at gmail.com
Mon Mar 21 06:03:44 EDT 2011


Hiya,

I'm new to Android dev, so I'm prob doing something stupid, but I'm having
some difficulty writing a custom adapter for a ListView.  I use an
Android.Runtime.JavaList collection to hold my bound items, but whenever I
try to retrieve any values from it I always get null values even though I've
added valid objects.  As a result, my GetView method is not correctly
populating my list view.  Here is my adapter:

    public class SimpleMessageAdapter : BaseAdapter
    {
        private readonly Context _context;
        private readonly IList<SimpleMessage> _list;

        public SimpleMessageAdapter(Context context, IList<SimpleMessage>
list)
        {
            _context = context;
            _list = list;
        }

        public override Object GetItem(int position)
        {
            return _list[position];
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override View GetView(int position, View convertView,
ViewGroup parent)
        {
            TextView tv = convertView as TextView ?? new TextView(_context);
            tv.Text = _list[position] != null ? _list[position].Text :
"(error)";       // _list[position] is *always* null here
            return tv;
        }

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

And here is my Activity, for reference:

    [Activity(Label = "AndroidClient", MainLauncher = true)]
    public class MessagingActivity : Activity
    {
        private MessageChannel _channel;
        private IList<SimpleMessage> messageList = new
JavaList<SimpleMessage>();
        private SimpleMessageAdapter _adapter;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            var messages = FindViewById<ListView>(Resource.Id.messages);
            _adapter = new SimpleMessageAdapter(this, messageList);
            messages.Adapter = _adapter;

            _channel = new MessageChannel();
            _channel.ReceivedMessages
                .OfType<SimpleMessage>()
                .Subscribe(msg => AddMessage(msg));

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button>(Resource.Id.send_button);
            button.Click += delegate {
SendMessage(FindViewById<EditText>(Resource.Id.new_message).Text.ToString());
};

            Button button2 = FindViewById<Button>(Resource.Id.poll_button);
            button2.Click += delegate { _channel.ForcePoll(); };
        }

        private void AddMessage(SimpleMessage msg)
        {
            RunOnUiThread(() =>
                              {
                                  messageList.Add(msg);     // This is where
I add my objects into the list
                                  _adapter.NotifyDataSetChanged();
                              });
        }

        private void SendMessage(string message)
        {
            _channel.Send(new SimpleMessage
                              {
                                  Text = message
                              });
        }
    }

All help appreciated.

Cheers,
John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/mailman/private/monodroid/attachments/20110321/81c47b2e/attachment.html 


More information about the Monodroid mailing list