Я пытаюсь сохранить / получить объект, когда нажимаю на кнопку в строке в ListView.
Итак, это мой код для щелчка по строке ListView, которая работает как задумано
//Listview
ListView mainListView = FindViewById<ListView>(Resource.Id.lstViewMain);
//Custom adapter (taking a list "allComponents")
ComponentListViewAdapter adapter = new ComponentListViewAdapter(this, allComponents);
mainListView.Adapter = adapter;
//Listview item/row click
mainListView.ItemClick += MainListView_ItemClick;
//Function to show the component's notes
private void MainListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
//Get the component tapped
var component = allComponents[(int)e.Id];
//Display message that the component was tapped
Toast.MakeText(this, component.Name.ToString() + " Tapped", ToastLength.Long).Show();
// Declare the activity as intent
var intent = new Intent(this, typeof(View_NotesActivity));
//Transfer the component's notes to the notes screen for display
intent.PutExtra("component_notes", component.Notes);
//Start the activity of intent
StartActivity(intent);
}
Хотя при реализации той же логики c для нажатия кнопки в строке ListView
//Edit button
ImageButton btnEditComponent = FindViewById<ImageButton>(Resource.Id.imgBtnEditComponent);
btnEditComponent.Click += BtnEditComponent_Click;
private void BtnEditComponent_Click(object sender, EventArgs e)
{
//throw new NotImplementedException();
}
я получаю ошибку "System.NullReferenceException: 'Ссылка на объект не установлена для экземпляра объекта.' '
Мне удалось реализовать кнопку редактирования в пользовательском классе адаптера ListView, но я не знаю, как получить доступ к «AdapterView.ItemClickEventArgs e», как в функция MainListView_ItemClick для сохранения объекта / компонента в переменной, снова как в функции MainListView_ItemClick, следующим образом:
//Function to show the component's notes
private void MainListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
//Get the component tapped
var component = allComponents[(int)e.Id];
Это две попытки реализации кнопки «Редактировать» (кнопка на ListView) в пользовательский класс адаптера ListView, каким образом я знаю, как получить доступ к / сохранить компонент, на котором кнопка редактирования для данной строки.
//Edit component button
editComponent = row.FindViewById<ImageButton>(Resource.Id.imgBtnEditComponent);
editComponent.Click += EditComponent_Click;
Попытка 1:
private void EditComponent_Click(object sender, EventArgs e)
{
//Get the component tapped
//This does not work like it does with the ListView row tap function.
//var component = allComponents[(int)e.Id];
Toast.MakeText(mContext, e.ToString() + " Edited", ToastLength.Long).Show();
//Retuns "System.EventArgs Edited"
//throw new NotImplementedException();
}
Попытка 2:
//Set on click listener for the Edit component Button
editComponent.SetOnClickListener(this);
public void OnClick(View v)
{
switch (v.Id)
{
case Resource.Id.imgBtnEditComponent:
//How can I grab the component when the only argument is a View?
break;
}
Большое спасибо