Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Changing a specific value in a list during runtime.

$
0
0

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


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>