[英]Xamarin Android C#: Populate ListView Custom Row With Data From SQLite Database
[英]How to populate a Listview in xamarin android c# using REST API, custom listview, adapter, row layout
我一般的編程知識非常有限。 嘗試創建自定義列表視圖以主要顯示圖像和消息。
我一直在嘗試遵循有關如何做的教程,但終生無法解決如何使其工作..
當前有這些錯誤,不知道如何解決:
[![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;
}
}
}
List<Message> message = JsonConvert.DeserializeObject<List<Message>>(json);
message已經是消息List<Message>
因此您無需創建新消息。 只需將此對象傳遞給適配器構造函數即可。
您可以刪除此部分:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.