Hello altogether,
In my program, I have a button and a list displayed. When clicking on the button, I want simply change the value of a specific item within that list. Please see following aimed output example:
[Program Start]
[Button Click]
So how can I do this? What does my private void OnButtonClick(object sender, EventArgs e)-method have to look like? How can I access a specific entry in that list?
To be better able to help me, I please let me share my full code following:
Using following *.axml-files:
So how can I access the appropriate view/change it inside the private void OnButtonClick(object sender, EventArgs e)-method?
If I do
only the first item changes. So how to pass the position through the adapter? How to change (in this example) only the second one (as shown in the beginning)? Can anyone maybe help?
Thanks in advance,
Best regards
In my program, I have a button and a list displayed. When clicking on the button, I want simply change the value of a specific item within that list. Please see following aimed output example:
[Program Start]
Name | Value |
Item Number 1 | Original Value |
Item Number 2 | Original Value |
Item Number 3 | Original Value |
[Button Click]
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)-method have to look like? How can I access a specific entry in that list?
To be better able to help me, I please let me share my full code following:
C#:
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;
}
}
C#:
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;
}
}
C#:
<?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>
C#:
<?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>
If I do
C#:
private void OnButtonClick(object sender, EventArgs e)
{
FindViewById(Resource.Id.value).Text = "Changed Value";
}
Thanks in advance,
Best regards