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:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android ="http://schemas.android.com/apk/res/android"
- android:orientation ="vertical"
- android:layout_width ="match_parent"
- android:layout_height="match_parent">
- <Button
- android:layout_width ="match_parent"
- android:layout_height="wrap_content"
- android:text="Click Me"
- android:id="@+id/button" />
- <ListView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/list" />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView
- android:id="@+id/name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView
- android:id="@+id/value"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- </RelativeLayout>
When simply doing
- private void OnButtonClick(object sender, EventArgs e)
- {
- FindViewById(Resource.Id.value).Text = "Changed Value";
- }
only the first item changes. So how to pass the position through the adapter? Can anyone maybe help?
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.