Danny Bunschoten
Danny Bunschoten

Reputation: 15

show one object of a class in listview C#

I am making an app with xamarin in c#. this is my code:

ArrayAdapter<Skylander> adapter;    
ListView List = FindViewById<ListView>(Resource.Id.LstSkylanders);
List.Adapter = adapter;

my class:

class Skylander
        {
            public string Name { get; set; }
            public int Power { get; set; }
            public int Protection { get; set; }
            public int Speed { get; set; }
            public int Luck { get; set; }

            public override string ToString()
            {
                string StringName= "Name: " + Name;
                return StringName;
            }
        };

My function to make a string to number:

public int ToNumber(string number)
        {
            int temp;
            try
            {
                temp = Convert.ToInt32(number);
            } catch { temp = 0; };
            return temp;
        }

And this is how I add a Skylander to the list:

Skylander New = new Skylander { Name = Name.Text, Power = ToNumber(Power.Text), Protection = ToNumber(Protection.Text),Speed = ToNumber(Speed.Text), Luck = ToNumber(Luck.Text)};

                adapter.Add(New.ToString());

This is my output:Output

I am trying to only show the name in my listview. I hope you can help me.

Danny

Upvotes: 2

Views: 306

Answers (1)

Artem Zelinskiy
Artem Zelinskiy

Reputation: 2210

Inherit from Java.Lang.Object, as Xamarin compiler uses java version of toString, leave everything else untouched

 class Skylander: Java.Lang.Object{

    .....

     public override string ToString()
                {
                    string StringName= "Name: " + Name;
                    return StringName;
                }

    }

Upvotes: 1

Related Questions