【问题标题】:How to populate a Listview in xamarin android c# using REST API, custom listview, adapter, row layout如何使用 REST API、自定义列表视图、适配器、行布局在 xamarin android c# 中填充列表视图
【发布时间】:2017-05-23 06:54:53
【问题描述】:

一般来说,我对编程的了解非常有限。尝试创建一个自定义列表视图主要显示图像和消息。

我一直在尝试遵循有关如何做到这一点的教程,但我一生都无法弄清楚如何让它发挥作用..

目前有这些错误,不知道如何修复:

[![msgInbox 屏幕错误][1]][1]

这是带有列表的 msgInbox 活动:

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Dribl;
using Newtonsoft.Json;

namespace Dribl.Droid
{
    [Activity(Label = "MsgInbox", Theme = "@style/CustomActionBarTheme")]
    public class MsgInbox : Activity
    {

        TextView txt;

        //Button backBtn;

        private List<Message> msgItems;

        private ListView msgListview;

        //private BaseAdapter<msgInfo> mAdapter;

        //action bar layout buttons
        LinearLayout surveysBtn;


        LinearLayout availabilityBtn;


        LinearLayout inboxBtn;


        LinearLayout dashboardBtn;




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

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.msgInbox);
            //add the action bar to the layout 
            ActionBar.SetCustomView(Resource.Layout.action_bar);
            ActionBar.SetDisplayShowCustomEnabled(true);



            //actionbar layout btns
            //actionbar layout btns
            surveysBtn = FindViewById<LinearLayout>(Resource.Id.SurveyLayout);
            surveysBtn.Click += surveyBtn_Click;

            inboxBtn = FindViewById<LinearLayout>(Resource.Id.InboxLayout);
            inboxBtn.Click += InboxBtn_Click;


            availabilityBtn = FindViewById<LinearLayout>(Resource.Id.availabilityLayout);
            availabilityBtn.Click += availabilityBtn_Click;

            dashboardBtn = FindViewById<LinearLayout>(Resource.Id.dashboardLayout);
            dashboardBtn.Click += dashboardBtn_Click;


            txt = FindViewById<TextView>(Resource.Id.msg_txt);


            WebClient client = new WebClient();
            System.Uri uri = new System.Uri("http://dribl.com/api/getAllMyMessages");
            NameValueCollection parameters = new NameValueCollection();


            parameters.Add("token", GlobalVariables.token);

            client.UploadValuesAsync(uri, parameters);
            client.UploadValuesCompleted += client_UploadValuesCompleted;

            //listview
            msgListview = FindViewById<ListView>(Resource.Id.msgListView);
            msgListview.ItemClick += MsgListview_ItemClick;


        }

        private void MsgListview_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
        {
            Intent intent = new Intent(this, typeof(msgDetails));

            //add in a variable to store the message that was clicked on and pass across to next pages txtfield
            intent.PutExtra("msgDet", "This is a message");
            StartActivity(intent);
        }

        protected internal void client_UploadValuesCompleted(object sender, UploadValuesCompletedEventArgs e)
        {
            string json = Encoding.UTF8.GetString(e.Result);
            List<Message> messages = JsonConvert.DeserializeObject<List<Message>>(json);

            //display the retrieved msg in the console output
            //Console.WriteLine(message[1].message + " is the message");

            //display the msg in a text view at top of page
            //txt.Text = message[1].message;


            //get the list view create a string to store and add to the list view based on the json return

            // msgItems = new List<Message>();

            // for (int c = 0; c < message.Count; c++)
            // {

            //     msgItems.Add(message[c].message);
            //msgItems.Add(new Message() { message = "pauls hectic"});
            //}

            msgItems = messages;

           // msgAdapter msgAdapter = new msgAdapter(this, msgItems);
            //msgListview.Adapter = msgAdapter;


            //Msgs.Add(message[1].message);
            //Msgs.Add(message[0].message);

           // ArrayAdapter<Message> adapter = new ArrayAdapter<Message>(this, Android.Resource.Layout.SimpleListItem1, msgItems);
            //msgListview.Adapter = adapter;
           // msgAdapter msgAdapter = new MsgAdapter(this, message);
            //var msgAdapter = new MsgAdapter(this, message);
            MsgAdapter msgAdapter = new MsgAdapter(this, messages);
        }


        void surveyBtn_Click(object sender, EventArgs e)
        {
            Intent intent = new Intent(this, typeof(Surveys));
            this.StartActivity(intent);
            this.Finish();
        }

        void dashboardBtn_Click(object sender, EventArgs e)
        {
            Intent intent = new Intent(this, typeof(dashboard));
            this.StartActivity(intent);
            this.Finish();
        }

        void availabilityBtn_Click(object sender, EventArgs e)
        {
            Intent intent = new Intent(this, typeof(Availability));
            this.StartActivity(intent);
            this.Finish();
        }

        void InboxBtn_Click(object sender, EventArgs e)
        {
            Intent intent = new Intent(this, typeof(MsgInbox));
            this.StartActivity(intent);
            this.Finish();
        }





    }


}

这是 msgInfo 类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace Dribl.Droid
{

            public class Message
        {

            public string messages { get; set; }


    }

}

这是味精适配器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace Dribl.Droid
{


    class MsgAdapter : BaseAdapter<Message>
    {
        private List<Message> msgItems;
        private Context msgContext;

        public MsgAdapter(Context Context, List<Message> Items)
        {
            msgItems = Items;
            msgContext = Context;
        }
        public override int Count
        {
            get { return msgItems.Count; }
        }
        public override long GetItemId(int position)
        {
            return position;
        }
        public override Message this[int position]
        {
            get { return msgItems[position]; }
        }
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View row = convertView;

            if (row == null)
            {
                row = LayoutInflater.From(msgContext).Inflate(Resource.Layout.msgCell, parent, false);
            }


            TextView message = row.FindViewById<TextView>(Resource.Id.message);
            message.Text = msgItems[position].messages;

            return row;
        }
    }
}

【问题讨论】:

    标签: c# android listview xamarin android-arrayadapter


    【解决方案1】:
    List<Message> message = JsonConvert.DeserializeObject<List<Message>>(json);
    

    message 已经是List&lt;Message&gt; 的消息列表,因此您无需创建新消息。只需将此对象传递给适配器构造函数。

    您可以删除这部分:

           msgItems = new List<Message>();
    
            for (int c = 0; c < message.Count; c++)
            {
                msgItems.Add(message[c].message);
            }
    

    但如果你想保存消息列表,你只需要:

    msgItems = messages;
    

    然后创建你的适配器对象:

    msgAdapter adapter = new msgAdapter(this, message);
    

    或者你也可以

    var adapter = new msgAdapter(this, message);
    

    并添加适配器:

    msgListview.Adapter = adapter;
    

    注意事项:

    message 变量更改为 messages,因为它包含不止一条消息。

    msgAdapter 将名称更改为 MsgAdapter(第一个大写字母),这样您就可以轻松找出什么时候是类以及什么时候是对象。

    更新:

    您的适配器的GetView 方法有错误。

    膨胀行时需要指明父级:

    row = LayoutInflater.From(msgContext).Inflate(Resource.Layout.msgCell, parent, false);
    

    【讨论】:

    • 我按照你说的做了改变,但是对于:var msgAdapter = new MsgAdapter(this, message);
    • 它说找不到“Msgadapter”类型或命名空间
    • 正如我的笔记所示,您需要更改您的适配器类名称...将更新答案
    • 是的,我这样做了,但后来意识到我有不同的名称空间和一个额外的名称空间。从另一个解决方案转移。该错误现在消失了,但列表视图现在显示为空白.. ???
    • 在创建 Adapter 对象之前放置一个断点,并确认您实际上是从后端接收数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-02
    • 1970-01-01
    • 2011-10-06
    • 2013-11-06
    • 1970-01-01
    相关资源
    最近更新 更多