我有cforbeginners的以下代码示例,我正在尝试更新现有数组,然后使用该数组从单独的类更新using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Support.V7.Widget;
using System.Collections.Generic;
namespace RecyclerViewTutorial
{
[Activity(Label = "RecyclerViewTutorial", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
private RecyclerView.Adapter mAdapter;
private MyList mEmails;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
mRecyclerView = FindViewById(Resource.Id.recyclerView);
mEmails = new MyList();
mEmails.Add(new Email() { Name = "tom", Subject = "Wanna hang out?", Message = "I'll be around tomorrow!!" });
mEmails.Add(new Email() { Name = "tom", Subject = "Wanna hang out?", Message = "I'll be around tomorrow!!" });
mEmails.Add(new Email() { Name = "tom", Subject = "Wanna hang out?", Message = "I'll be around tomorrow!!" });
mEmails.Add(new Email() { Name = "tom", Subject = "Wanna hang out?", Message = "I'll be around tomorrow!!" });
//Create our layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.SetLayoutManager(mLayoutManager);
mAdapter = new RecyclerAdapter(mEmails, mRecyclerView);
mEmails.Adapter = mAdapter;
mRecyclerView.SetAdapter(mAdapter);
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.actionbar, menu);
return base.OnCreateOptionsMenu(menu);
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
switch(item.ItemId)
{
case Resource.Id.add:
//Add button clicked
mEmails.Add(new Email() { Name = "New Name", Subject = "New Subject", Message = "New Message" });
return true;
case Resource.Id.discard:
//Discard button clicked
mEmails.Remove(mEmails.Count - 1);
return true;
}
return base.OnOptionsItemSelected(item);
}
}
public class MyList
{
private List mItems;
private RecyclerView.Adapter mAdapter;
public MyList()
{
mItems = new List();
}
public RecyclerView.Adapter Adapter
{
get { return mAdapter; }
set { mAdapter = value; }
}
public void Add(T item)
{
mItems.Add(item);
if (Adapter != null)
{
Adapter.NotifyItemInserted(0);
}
}
public void Remove (int position)
{
mItems.RemoveAt(position);
if (Adapter != null)
{
Adapter.NotifyItemRemoved(0);
}
}
public T this[int index]
{
get { return mItems[index]; }
set { mItems[index] = value; }
}
public int Count
{
get { return mItems.Count; }
}
}
public class RecyclerAdapter : RecyclerView.Adapter
{
private MyList mEmails;
private RecyclerView mRecyclerView;
public RecyclerAdapter(MyList emails, RecyclerView recyclerView)
{
mEmails = emails;
mRecyclerView = recyclerView;
}
public class MyView : RecyclerView.ViewHolder
{
public View mMainView { get; set; }
public TextView mName { get; set; }
public TextView mSubject { get; set; }
public TextView mMessage { get; set; }
public MyView (View view) : base(view)
{
mMainView = view;
}
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
View row = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.row, parent, false);
TextView txtName = row.FindViewById(Resource.Id.txtName);
TextView txtSubject = row.FindViewById(Resource.Id.txtSubject);
TextView txtMessage = row.FindViewById(Resource.Id.txtMessage);
MyView view = new MyView(row) { mName = txtName, mSubject = txtSubject, mMessage = txtMessage };
return view;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
MyView myHolder = holder as MyView;
int indexPosition = (mEmails.Count - 1) - position;
myHolder.mMainView.Click += mMainView_Click;
myHolder.mName.Text = mEmails[indexPosition].Name;
myHolder.mSubject.Text = mEmails[indexPosition].Subject;
myHolder.mMessage.Text = mEmails[indexPosition].Message;
}
void mMainView_Click(object sender, EventArgs e)
{
int position = mRecyclerView.GetChildPosition((View)sender);
int indexPosition = (mEmails.Count - 1) - position;
Console.WriteLine(mEmails[indexPosition].Name);
}
public override int ItemCount
{
get { return mEmails.Count; }
}
}
}
。具体来说,我试图从后台服务更新。我一直试图找到一个有效的例子。
主要活动。
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 RecyclerViewTutorial
{
public class Email
{
public string Name { get; set; }
public string Subject { get; set; }
public string Message { get; set; }
public string Time { get; set; }
public bool Favorite { get; set; }
}
}
Email.cs
{{1}}