Xamarin passing context from OnClick event - c#

I m a beginner in android dev, I m struggling with passing string Clicked_Message from Click event in Recycle Adapter Class to the other activity. Is it a good way to use Intent? If so how can I pass context to click event? Thanks
public class RecyclerAdapter : RecyclerView.Adapter
{
private RecyclerView mRecyclerView;
private List<NotificationClass> mEmails;
public RecyclerAdapter(List<NotificationClass> 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<TextView>(Resource.Id.txtName);
TextView txtSubject = row.FindViewById<TextView>(Resource.Id.txtSubject);
TextView txtMessage = row.FindViewById<TextView>(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[position].Name;
myHolder.mSubject.Text = mEmails[position].Subject;
myHolder.mMessage.Text = mEmails[position].Message;
}
public override int ItemCount
{
get { return mEmails.Count; }
}
public void OnClick(int position)
{
if (ItemClick != null)
ItemClick(this, position);
}
public void mMainView_Click(object sender, EventArgs e,Context context)
{
int position = mRecyclerView.GetChildPosition((View)sender);
int indexPosition = (mEmails.Count - 1) - position;
Console.WriteLine(mEmails[indexPosition].Message);
string Clicked_Message = (mEmails[indexPosition].Message);
var activity2 = new Intent(context, typeof(ContactActivity));
activity2.PutExtra("MyData", Clicked_Message);
context.StartActivity(activity2);
}
}

You don't need to pass a context. Just use an intent and put the information you want to pass as extras into the intent.
In case your adapter needs a context, pass it in through the constructor and store it as a field member.

This is my typical implementation of the RecyclerView.Adapter with a view holder...
public class ContactsAdapter : V7.RecyclerView.Adapter
{
private List<Contact> _contacts;
public event EventHandler ItemClick;
public void OnItemClick(ContactViewHolder holder)
{
if (ItemClick != null)
{
ItemClick(holder, EventArgs.Empty);
}
}
public ContactsAdapter(List<Contact> contacts)
: base()
{
_contacts = contacts;
}
public override void OnBindViewHolder(V7.RecyclerView.ViewHolder holder, int position)
{
var contactHolder = (ContactViewHolder)holder;
contactHolder.BindUI(_contacts[position]);
}
public override V7.RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
var view = LayoutInflater.FromContext(parent.Context).Inflate(Resource.Layout.ContactsListItem, parent, false);
return new ContactViewHolder(view)
{
Adapter = this
};
}
public override int ItemCount
{
get
{
return _contacts.Count;
}
}
}
View Holder (typically in the same file as the adapter)
public class ContactViewHolder : V7.RecyclerView.ViewHolder, View.IOnClickListener
{
public TextView ContactNameTextView { get; set; }
public TextView ContactPhoneTextView { get; set; }
public TextView ContactIntialsTextView { get; set; }
public Contact Contact { get; set; }
private WeakReference _adapter;
public ContactsAdapter Adapter
{
get { return (ContactsAdapter)_adapter.Target; }
set { _adapter = new WeakReference(value); }
}
public ContactViewHolder(View view)
: base(view)
{
GetUI(view);
view.SetOnClickListener(this);
}
private void GetUI(View view)
{
ContactNameTextView = view.FindViewById<TextView>(Resource.Id.ContactName);
ContactPhoneTextView = view.FindViewById<TextView>(Resource.Id.ContactPhone);
ContactIntialsTextView = view.FindViewById<TextView>(Resource.Id.ContactInitialsTextView);
}
public void BindUI(Contact contact)
{
Contact = contact;
ContactNameTextView.Text = contact.ContactName;
ContactPhoneTextView.Text = contact.Phone1;
ContactIntialsTextView.Text = contact.Initials;
}
public void OnClick(View v)
{
Adapter.OnItemClick(this);
}
}
This encapsulates the functionality to the view holder. I also give the instance of the adapter to the view holder as a WeakReference. This allows me to call the OnItemClick, passing the instance of the view holder. If you will notice that the view holder also has an instance of the object that it is representing. This means I don't have to worry about the index that was chosen. I already have the object data available to me.
So the implementation in the Activity/Fragment is like this...
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
_contacts = Contact.GetAllContacts();
_adapter = new ContactsAdapter(_contacts);
_adapter.ItemClick += ContactSelected;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.ContactsFragment, container, false);
var layoutManager = new V7.LinearLayoutManager(this.Activity) { Orientation = V7.LinearLayoutManager.Vertical };
_contactsView = view.FindViewById<V7.RecyclerView>(Resource.Id.ContactList);
_contactsView.SetAdapter(_adapter);
_contactsView.HasFixedSize = true;
_contactsView.SetLayoutManager(layoutManager);
return view;
}
private void ContactSelected (object sender, EventArgs e)
{
var holder = (ContactViewHolder)sender;
var detailFragment = new ContactDetailsFragment(holder.Contact);
MainActivity.ShowFragment(detailFragment);
}
I give the Contact to a Fragment, but you could do something similar for an activity using an intent.
Now whether this is the most efficient way of handling a click of a row in a RecyclerView, I don't know. But this implementation has been working for me.

Related

C# Xamarin.Android Calling Dialogfragment from recyclerView

I would like to display diaglog fragment by clicking a button/imageView (By clicking the sAddButton)from recyclerView. But when I clicked the button nothing shows up.
How do I achieve that here is my code.
My RecyclerView Adapter
I added an pulic eventhandler in myadapter (StockInItemClick)
then created a delegate method to invoke the eventhandler(OnStockInClick)
then pass the delegate method as parameter on my adapterview holder
then Implement the delegate method on my adapterview holder.
internal class StocksAdapter : RecyclerView.Adapter
{
public event EventHandler<StocksAdapterClickEventArgs> ItemClick;
public event EventHandler<StocksAdapterClickEventArgs> ItemLongClick;
public event EventHandler<StocksAdapterClickEventArgs> StockInItemClick;
List<Products> items;
public StocksAdapter(List<Products> data)
{
items = data;
}
// Create new views (invoked by the layout manager)
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
//Setup your layout here
View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.stock_rView, parent, false);
//var id = Resource.Layout.__YOUR_ITEM_HERE;
//itemView = LayoutInflater.From(parent.Context).
// Inflate(id, parent, false);
var vh = new StocksAdapterViewHolder(itemView, OnClick, OnLongClick, OnStockInClick);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
public override void OnBindViewHolder(RecyclerView.ViewHolder viewHolder, int position)
{
var holder = viewHolder as StocksAdapterViewHolder;
//holder.TextView.Text = items[position];
holder.sNameText.Text = items[position].PName;
holder.sQtyText.Text = items[position].QtyonHand;
}
public override int ItemCount => items.Count;
void OnClick(StocksAdapterClickEventArgs args) => ItemClick?.Invoke(this, args);
void OnLongClick(StocksAdapterClickEventArgs args) => ItemLongClick?.Invoke(this, args);
void OnStockInClick(StocksAdapterClickEventArgs args) => StockInItemClick?.Invoke(this, args);
}
public class StocksAdapterViewHolder : RecyclerView.ViewHolder
{
//public TextView TextView { get; set; }
public TextView sNameText { get; set; }
public TextView sQtyText { get; set; }
public ImageButton sAddButton { get; set; }
public StocksAdapterViewHolder(View itemView, Action<StocksAdapterClickEventArgs> clickListener,
Action<StocksAdapterClickEventArgs> longClickListener,
Action<StocksAdapterClickEventArgs> stockInClickListener) : base(itemView)
{
//TextView = v;
sNameText = (TextView)itemView.FindViewById(Resource.Id.sNameTView);
sQtyText = (TextView)itemView.FindViewById(Resource.Id.qtyonhandTView);
sAddButton = (ImageButton)itemView.FindViewById(Resource.Id.addButton);
itemView.Click += (sender, e) => clickListener(new StocksAdapterClickEventArgs { View = itemView, Position = AdapterPosition });
itemView.LongClick += (sender, e) => longClickListener(new StocksAdapterClickEventArgs { View = itemView, Position = AdapterPosition });
sAddButton.Click += (sender, e) => longClickListener(new StocksAdapterClickEventArgs { View = itemView, Position = AdapterPosition });
}
}
public class StocksAdapterClickEventArgs : EventArgs
{
public View View { get; set; }
public int Position { get; set; }
}
my code in activity
I implement the eventhandler
then on the event onlcick event(Adapter_StockInItemClick) I called the
dialog fragment.
private void SetupRecyclerView()
{
stockRView.SetLayoutManager(new Android.Support.V7.Widget.LinearLayoutManager(stockRView.Context));
adapter = new StocksAdapter(StockList);
adapter.StockInItemClick += Adapter_StockInItemClick;
stockRView.SetAdapter(adapter);
}
private void Adapter_StockInItemClick(object sender, StocksAdapterClickEventArgs e)
{
Products thisproducts = StockList[e.Position];
addNewStockInProductsFragment = new AddNewStockInProductsFragment(thisproducts);
var trans = SupportFragmentManager.BeginTransaction();
addNewStockInProductsFragment.Show(trans, "StockIn");
}
Found the problem
sAddButton.Click += (sender, e) => longClickListener(new StocksAdapterClickEventArgs { View = itemView, Position = AdapterPosition });
shouldbe (deleteClickListener) not (longClickListener)
sAddButton.Click += (sender, e) => deleteClickListener(new StocksAdapterClickEventArgs { View = itemView, Position = AdapterPosition });

Check custom listview items on Activity start

I have a custom Listview in my C# Android, each row contains one textview and one checkbox. I am saving the value (or the text) of the selected row's textview in a public list named usercoin. Each time the user opens the app, the list usercoin will contain the text of the his textview selected items, and I am doing that using SQLite. The problem is I want to re-check the items which the user have previously selected automatically when the activity starts which are stored in the usercoin list as I have previously noted. But I am not able to achieve this.
MainActivity.cs
ListView mListView;
MyAdapter adapter;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
mListView = FindViewById<ListView>(Resource.Id.listview);
List<TableList> list = new List<TableList>();
list.Add(new TableList("Germany",false));
list.Add(new TableList("France", false));
list.Add(new TableList("Finland", false));
list.Add(new TableList("Germany", false));
list.Add(new TableList("France", false));
list.Add(new TableList("Germany", false));
list.Add(new TableList("France", false));
list.Add(new TableList("Finland", false));
adapter = new MyAdapter(this, list);
mListView.Adapter = adapter;
mListView.ItemClick += MListView_ItemClick;
}
private void MListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
var t = list[e.Position];
string selected = t.name;
var ll = e.View as LinearLayout;
var cb = ll.GetChildAt(2) as CheckBox;
if (cb.Checked)
{
cb.Checked = false;
adapter.changeState((int)cb.Tag, false);
}
else
{
cb.Checked = true;
adapter.changeState((int)cb.Tag, true);
}
}
class MyAdapter : BaseAdapter
{
Context mContext;
List<TableList> mitems;
public MyAdapter(Context context, List<TableList> list)
{
this.mContext = context;
this.mitems = list;
}
public override int Count
{
get
{
return mitems.Count;
}
}
public override Java.Lang.Object GetItem(int position)
{
return mitems[position];
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
DataViewHolder holder = null;
if (convertView == null)
{
convertView = LayoutInflater.From(mContext).Inflate(Resource.Layout.CoinList, null, false);
holder = new DataViewHolder();
holder.tv = convertView.FindViewById<TextView>(Resource.Id.CoinName);
holder.iv = convertView.FindViewById<ImageView>(Resource.Id.imageView1);
holder.cb = convertView.FindViewById<CheckBox>(Resource.Id.checkBox1);
convertView.Tag = holder;
}
else
{
holder = convertView.Tag as DataViewHolder;
}
holder.cb.Tag = position;
holder.tv.Text = mitems[position].Name;
holder.cb.Focusable = false;
holder.cb.Checked = mitems[position].bl;
holder.iv.SetImageResource(Resource.Drawable.dapao);
holder.cb.CheckedChange += Cb_CheckedChange;
return convertView;
}
private void Cb_CheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
{
var cb = sender as CheckBox;
if (e.IsChecked && !mitems[(int)cb.Tag].bl)
{
mitems[(int)cb.Tag].bl = true;
this.NotifyDataSetChanged();
}
else if (!e.IsChecked && mitems[(int)cb.Tag].bl)
{
mitems[(int)cb.Tag].bl = false;
this.NotifyDataSetChanged();
}
}
internal void changeState(int tag, bool v)
{
mitems[tag].bl = v;
this.NotifyDataSetChanged();
}
}
public class DataViewHolder : Java.Lang.Object
{
public ImageView iv { get; set; }
public TextView tv { get; set; }
public CheckBox cb { get; set; }
}
public class TableList : Java.Lang.Object
{
private string v;
public TableList(string name, bool b)
{
this.Name = name;
this.bl = b;
}
public string Name { get; set; }
public bool bl { get; set; }
}
}
}
For example, when the user first run the app and select France and Germany from the listview, next time he re-opens the app, the usercoin list will contain France and Germany. Now the question is how can I check the checkboxes corresponding to those values in the listview. I have tried to do so by including this code in MyAdapter : BaseAdapter class:
if (Class1.usercoin.Contains(item.CoinAbr))
{
Class1.adapter[(int)holder.cb.Tag].bl = true;
this.NotifyDataSetChanged();
}
But when this code get executed, the previously checked items are checked plus some other items which the user haven't checked previously are also checked. So how can I check the previously checked items in the Listview on the app start ? Please help me to find a solution.

How do we create a MvxRecyclerView as a child inside a Parent MvxRecyclerView using MVVMCross Android

In my project , i want to create a MvxRecyclerView to show all the products which user purchased on particular dates.So i have a main MvxRecyclerView layout and a Adapter class which extends MvxRecyclerAdapter .Inside this MvxRecyclerAdapter , iam creating a layouts which contains MvxRecyclerView which populates all the items based on date.
When i tried to create the adapter, Mvvmcross is not showing the child adapter data since OnCreateViewHolder of the child adapter is throwing error.
Please check My parent Adapter class below:
public class MyRecyler1Adapter: MvxRecyclerAdapter
{
private Activity _activity;
public static int MEETING_TYPE = 0;
public static int DOCUMENTS_TYPE = 1;
public NewResponse _response;
Meeting1Adapter adapter;
Documents1Adapter docadapter;
public List<MeetingDetails> meetingDetailList { get; set; }
public List<DocumentDetails> docDetailsList { get; set; }
public bool btnDocStatus = false;
public NewViewModel viewModel;
public MyRecyler1Adapter(IMvxAndroidBindingContext bindingContext, Activity context, NewResponse response,NewViewModel model)
: base(bindingContext)
{
this._activity = context;
this._response = response;
this.viewModel = model;
}
public override int ItemCount
{
get { return 2; }
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
if (position == 0 && _response.Meetings != null)
{
var meeting = holder as MeetingsTypeViewHolder;
meeting.txtMeg.Text = "Meeting";_response.Meetings.Meetings[position].MeetingTitle+position);
adapter = new Meeting1Adapter(BindingContext, _activity, _response.Meetings);
meeting.recycle.SetAdapter(adapter);
meetingDetailList = _response.Meetings.Meetings;
}
else if (position == 1 && _response.Documents != null)
{
var documents = holder as DocumentsTypeViewHolder;
documents.txtMsg.Text = "Documents";
docadapter = new Documents1Adapter((IMvxAndroidBindingContext)BindingContext,_activity, _response.Documents);
documents.recycle.SetAdapter(docadapter);
docDetailsList = _response.Documents.Documents;
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
View itemView;
switch (viewType)
{
case 0:
if (_response.Meetings != null)
{
var itemBindingContext = new MvxAndroidBindingContext(parent.Context, this.BindingContext.LayoutInflaterHolder);
var id = Resource.Layout.layout1;
itemView = LayoutInflater.From(parent.Context).Inflate(id, parent, false);
return new MeetingsTypeViewHolder(itemView, itemBindingContext);
}
case 1:
if (_response.Documents != null)
{
var itemBindingContext = new MvxAndroidBindingContext(parent.Context, this.BindingContext.LayoutInflaterHolder);
var id1 = Resource.Layout.layout2;
itemView = LayoutInflater.From(parent.Context).Inflate(id1, parent, false);
return new DocumentsTypeViewHolder(itemView, itemBindingContext);
}
}
return null;
}
public class MeetingsTypeViewHolder : MvxRecyclerViewHolder
{
public TextView txtMeg { get; private set; }
public MvxRecyclerView recycle { get; set; }
public Button btnShowMore { get; set; }
public MeetingsTypeViewHolder(View itemView, IMvxAndroidBindingContext context) : base(itemView, context)
{
txtMeg = itemView.FindViewById<TextView>(Resource.Id.textView1);
recycle = itemView.FindViewById<MvxRecyclerView>(Resource.Id.rec1);
btnShowMore = itemView.FindViewById<Button>(Resource.Id.btnShowMore);
}
}
public class DocumentsTypeViewHolder : MvxRecyclerViewHolder
{
public TextView txtMsg { get; private set; }
public RecyclerView recycle { get; set; }
public Button btnShowMore { get; set; }
public DocumentsTypeViewHolder(View itemView, IMvxAndroidBindingContext context) : base(itemView, context)
{
txtMsg = itemView.FindViewById<TextView>(Resource.Id.textView1);
recycle = itemView.FindViewById<RecyclerView>(Resource.Id.rec1);
btnShowMore = itemView.FindViewById<Button>(Resource.Id.btnShowMore);
}
}
public override int GetItemViewType(int position)
{
switch (position)
{
case 0:
return MEETING_TYPE;
case 1:
return DOCUMENTS_TYPE;
default:
return -1;
}
}
}
Child adapter sample:
public class Meeting1Adapter: MvxRecyclerAdapter
{
private Activity activity;
public MeetingsMain meetingMain { get; set; }
public Meeting1Adapter(IMvxAndroidBindingContext bindingContext, Activity activity, MeetingsMain meetingsMain)
: base(bindingContext)
{
this.activity = activity;
this.meetingMain = meetingsMain;
}
public void RefreshItems(Object response)
{
if (response is List<ContactDetails>)
meetingMain.Meetings = (List<MeetingDetails>)response;
NotifyDataSetChanged();
}
public override int ItemCount
{
get { return meetingMain.Meetings.Count; }
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
var meeting = holder as MeetingsAdapterValueTypeViewHolder;
meeting.txtMeg.Text = meetingMain.Meetings[position].MeetingTitle;
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
try
{
var itemBindingContext = new MvxAndroidBindingContext(parent.Context, this.BindingContext.LayoutInflaterHolder);
var id = Resource.Layout.item_layout;
View itemView = LayoutInflater.From(parent.Context).Inflate(id, parent, false);
return new MeetingsAdapterValueTypeViewHolder(itemView, itemBindingContext);
}catch(Exception e)
{
Console.WriteLine("Exception in response ## " + e.Message);
}
return null;
}
public class MeetingsAdapterValueTypeViewHolder : MvxRecyclerViewHolder
{
// public ImageView Image { get; private set; }
public TextView txtMeg { get; private set; }
public MeetingsAdapterValueTypeViewHolder(View itemView, IMvxAndroidBindingContext ctxt) : base(itemView,ctxt)
{
txtMeg = itemView.FindViewById<TextView>(Resource.Id.textView);
}
}
}
}
Problem : In the child adapter ,OnCreateViewHolder () method is not calling.
Thanks in advance.
After 2 days in searching the solution ,finally i got the result.
The main thing we should check while loading MvxRecyclerview is
1: MvxRecyclerView.LayoutManager.
2:In Child Adapter,Since we are using MvxRecyclerView,we should use IMvxAndroidBindingContext
and the associated adapter changes like
a:Adapter should extend MvxRecyclerAdapter
b: use MvxRecyclerViewHolder
------------------------------------------Edit Your Adapter in the below mentioned way----
Code change made in MyRecyler1Adapter:
MvxRecyclerView.LayoutManager layoutManager;//Dont use RecyclerView.LayoutManager
Inside the OnBindViewHolder()
layoutManager = new LinearLayoutManager(_activity, LinearLayoutManager.Vertical, false);
meeting.recycle.SetLayoutManager(layoutManager);
Inside Child Adapter:
public class Meeting1Adapter: MvxRecyclerAdapter
{
private Activity activity;
public IMvxAndroidBindingContext bindingCtxt;
protected IMvxAndroidBindingContext BindingContext => bindingCtxt;
public MeetingsMain meetingMain { get; set; }
public Meeting1Adapter(IMvxAndroidBindingContext bindingContext, Activity activity, MeetingsMain meetingsMain)
{
this.bindingCtxt = bindingContext;
this.activity = activity;
this.meetingMain = meetingsMain;
}
----
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
try
{
var itemBindingContext = new MvxAndroidBindingContext(parent.Context, this.BindingContext.LayoutInflaterHolder);
var id = Resource.Layout.item_layout;
View itemView = LayoutInflater.From(parent.Context).Inflate(id, parent, false);
return new MeetingsAdapterValueTypeViewHolder(itemView, itemBindingContext);
// return new MeetingsAdapterValueTypeViewHolder(itemView);
}
catch (Exception e)
{
Console.WriteLine("Exception in response ## " + e.Message);
}
return null;
}
----
}

how to populate date from webservice to xamarin android

I can't populate date from webservice to xamarin android
namespace Printopack
{
[Activity(Label = "Mainlistview", Icon = "#drawable/icon")]
public class Mainlistview : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Mainlistview);
ListView ListView = FindViewById<ListView>(Resource.Id.listView1);
Selling.WebServiceDB ws = new Selling.WebServiceDB();
ws.OrderStatusListCompleted += Ws_OrderStatusListCompleted;
ws.OrderStatusListAsync(Convert.ToString(1));
}
private void Ws_OrderStatusListCompleted(object sender, Selling.OrderStatusListCompletedEventArgs e)
{
ListView ListView = FindViewById<ListView>(Resource.Id.listView1);
string msg = "";
if (e.Result.ToString().Equals("0"))
{
msg = e.Result.ToString();
}
else
{
// full class
List<TableItem> tableItems = new List<TableItem>();
tableItems.Add(new TableItem("" + e.Result, "" + e.Result, Resource.Drawable.Icon));
ListView.Adapter = new HomeScreenAdapter(this, tableItems);
}
}
// adpater manage
public class HomeScreenAdapter : BaseAdapter<TableItem>
{
List<TableItem> items;
Activity context;
public HomeScreenAdapter(Activity context, List<TableItem> items)
: base()
{
this.context = context;
this.items = items;
}
public override long GetItemId(int position)
{
return position;
}
public override TableItem this[int position]
{
get { return items[position]; }
}
public override int Count
{
get { return items.Count; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
View view = convertView;
if (view == null) // no view to re-use, create new
view = context.LayoutInflater.Inflate(Resource.Layout.Ticket_News, null);
view.FindViewById<TextView>(Resource.Id.Text1).Text = item.Heading;
view.FindViewById<TextView>(Resource.Id.Text2).Text = item.SubHeading;
view.FindViewById<ImageView>(Resource.Id.Image).SetImageResource(item.ImageResourceId);
return view;
}
}
public class TableItem
{
public string Heading;
public string SubHeading;
public int ImageResourceId;
public TableItem(string Heading, string SubHeading, int ImageResourceId)
{
this.Heading = Heading;
this.SubHeading = SubHeading;
this.ImageResourceId = ImageResourceId;
}
}
}
}
I think the error is in this line:
tableItems.Add(new TableItem("" + e.Result, "" + e.Result, Resource.Drawable.Icon));
You're adding the e.Result as one item and I think the Result holds the complete list. You should loop through the entries in the e.Result and call tableItems.Add for each of them.
Similar to this:
foreach (var item in e.Result)
{
tableItems.Add(new TableItem("" + item.Property, "" + item.Property1, Resource.Drawable.Icon));
}

RecyclerView don't have Item click and other events

Earlier I used ListView and there was lots of events in this class, but now I started to use RecyclerView and here I can't find any event.
for example:
RecyclerView mView = FindViewById<RecyclerView>(Resource.Id.recyclerView);
mView.ItemClick += delegate {
// action
};
is not working with this class - RecyclerView.
Actually these events whitch I want exist in RecyclerView.Adapter so I tried this:
public class ListItemArgs : EventArgs
{
public int itemPosition;
public ListItemArgs(int pos)
{
itemPosition = pos;
}
}
public class MyAdapter : RecyclerView.Adapter
{
public event EventHandler<ListItemArgs> OnItemLongClick;
// some overrides skipped (not needed anyway)
public class MyView : RecyclerView.ViewHolder
{
public View mMainView { get; set; }
public TextView mName { get; set; }
public TextView mEventCount { get; set; }
public CheckBox mFavorite { get; set; }
public MyView(View view) : base(view)
{
mMainView = view;
}
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
MyView mHolder = holder as MyView;
mHolder.mMainView.LongClick += delegate
{
Snackbar.Make(mRecyclerView, "Ar tikrai norite trinti pasirinkimÄ…?", Snackbar.LengthLong).Show();
OnItemLongClick.Invoke(mHolder.mMainView, new ListItemArgs(position));
};
}
}
and declarations in other class:
RecyclerView mView = FindViewById<RecyclerView>(Resource.Id.recyclerView);
RecyclerView.Adapter mAdapter new AddNewDayAdapter(mView);
And now I should be able to launch this event from mAdapter object, which leter I use to SetAdapter for my RecyclerView. (code below)
mAdapter.OnItemLongClick += (object sender, ListItemArgs e) =>
{
// action
}
mView.SetAdapter(mAdapter);
but mAdapter.OnItemLongClick is not working.
Main question: how can I get an event in which args would be view of row and selected item position ?

Categories