Changing a specific value in a list during runtime.

TBMSamTBMSam Member ✭✭

Hello everyone,

I have a simple Xamarin.Android-App.

On the Activity of this app, there is a list displayed. Please see following MyActivity.axml:

<?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:layout_marginTop   ="20dp"
    android:layout_marginLeft   ="25dp"
    android:layout_marginRight   ="25dp"
    android:id="@+id/button" />
<Space
    android:layout_width="match_parent"
    android:layout_height="25dp"
    android:id="@+id/space" />
<ListView
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/list" />
</LinearLayout>

One row of this list contains simply two entries, please see ListRow.axml:

<?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" 
        android:layout_marginLeft  ="5dp"
        android:layout_marginTop   ="10dp"
        android:layout_marginBottom="10dp"
        android:layout_marginRight ="5dp" />
    <TextView             
        android:id="@+id/value"            
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_marginLeft  ="5dp"
        android:layout_marginTop   ="10dp"
        android:layout_marginBottom="10dp"
        android:layout_marginRight ="5dp" />
</RelativeLayout>

So the code-behind of that activity looks like the following:

public class MyActivity : Activity
{
    List<Entry> List = null;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.MyActivity);

        List = PopulateList();
        var lv = FindViewById<ListView>(Resource.Id.list);
        var adapter = new ListAdapter(this, Resource.Layout.List, List);
        lv.Adapter = adapter;

        FindViewById<Button>(Resource.Id.button).Click += OnButtonClick;
    }
}

For the sake of completeness, here is the code for my ListAdapter.cs-class:

class ListAdapter : ArrayAdapter
{
    List<Entry> List;
    public ListAdapter(Context Context, int ListId, List<Entry> 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<TextView>(Resource.Id.name).Text = List[position].Name;
        v.FindViewById<TextView>(Resource.Id.value).Text = "Original Value";
        return v;
    }
}

So my question now is the following: Assuming that there is more than one item within that List. On click of the button, I want to change one specific text within that list. Let's say of the second entry in the list the (Resource.Id.value).Text (which now says "Original Value") to "Changed Value" or something like that. But only of the second one. All the other items should stay the same.

Please see following scenario, maybe it's easier to understand what I am trying to do:

NameValue
Item Number 1Original Value
Item Number 2Original Value
Item Number 3Original Value

[Button Click]

NameValue
Item Number 1Original Value
Item Number 2Changed Value
Item Number 3Original Value

Can anyone maybe help me / tell me how to do this? What does my private void OnButtonClick(object sender, EventArgs e)-method have to look like? How can I access a single entry in that list?

Thanks in advance for all answers and best regards

Answers

  • LeoZhuLeoZhu Member ✭✭✭

    If your button is outside of the listview, can you get an indication such as which position Item needs to change when you click on a Clicked event ? If can,you could define a method in the adapter, click on the button to pass position through the adapter, and then refresh the data to change the value of that item .

    or you could add a button to each listitem ,then click button to change the corresponding item's value

  • TBMSamTBMSam Member ✭✭
    edited February 4
    Hello @LeoZhu and thank you really much for your efforts helping me.

    As trying to show in the two tables above, in this example the position of the item which needs to change is `int positionToChange = 1` (starting to count with 0). Not the first item, not the last item, only the second one. How can I pass the position through the adapter?
    I found out that the refreshment is done with `adapter.NotifyDataSetChanged;`.
Sign In or Register to comment.