Hello everyone,
I have a button and a list displayed. When clicking on the button, I want to change the value of a specific item within that list. Please see following aimed output example:
[Program Start]
| Item Name | Value |
| Item Number 1 | Original Value |
| Item Number 2 | Original Value |
| Item Number 3 | Original Value |
[Button Click]
| Item Name | Value |
| Item Number 1 | Original Value |
| Item Number 2 | Changed Value |
| Item Number 3 | Original Value |
So how can I do this? What does my `
private void OnButtonClick(object sender, EventArgs e)` have to look like? How can I access a specific entry in that list? To be better able to help me, I will share my code following:
- public class MyActivity : Activity
- {
- List
List = null; - protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- SetContentView(Resource.Layout.MyActivity);
- List = PopulateList();
- var lv = FindViewById
(Resource.Id.list); - var adapter = new ListAdapter(this, Resource.Layout.List, List);
- lv.Adapter = adapter;
- FindViewById
(Resource.Id.button).Click += OnButtonClick; } - }
- class ListAdapter : ArrayAdapter
- {
- List
List; - public ListAdapter(Context Context, int ListId, List
List) : base(Context, ListId, List) - {
- this.List = List;
- }
- public override int Count
- {
- get { return List.Count; }
- }
- public override View GetView(int position, View convertView, ViewGroup parent)
- {
- View v = convertView;
- if (v == null)
- {
- LayoutInflater inflater = (LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);
- v = inflater.Inflate(Resource.Layout.List, parent, false);
- }
- v.FindViewById
(Resource.Id.name).Text = List[position].Name; - v.FindViewById
(Resource.Id.value).Text = "Original Value"; - return v;
- }
- }
Using following *.axml-files:
- "utf-8"?>
- xmlns:android ="http://schemas.android.com/apk/res/android"
- android:orientation ="vertical"
- android:layout_width ="match_parent"
- android:layout_height="match_parent">
- android:layout_width ="match_parent"
- android:layout_height="wrap_content"
- android:text="Click Me"
- android:id="@+id/button" />
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/list" />
"utf-8"?> - xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- android:id="@+id/name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- android:id="@+id/value"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
When simply doing
- private void OnButtonClick(object sender, EventArgs e)
- {
- FindViewById
(Resource.Id.value).Text = "Changed Value"; - }
Thanks in advance,
Best regards
Edit: I found out that I have to do `
adapter.NotifyDataSetChanged();` somewhere in the end. But when adding this, unfortunately nothing happens anymore when clicking the button.
Jefferson S. MottaPosted Feb 4, 2019, 6:43 PM