Creating a List<String> in Android Xamarin

17,962

Solution 1

   var projects = new List<String>() { "hey","yo","app","xamarin","c","xaml" };

Solution 2

if you are using the array to store what values you want in your list use the foreach

List<string>project = new List<string>();
string[] projects = { "hey","yo","app","xamarin","c","xaml" };

foreach(string str in projects)
{
   project.Add(str);
}
for (int i = 0; i < projects.Length; i++)
{
  // Inflate the tile
  var tile = LayoutInflater.Inflate (Resource.Layout.Tile, null);

  // Set its attributes
  tile.FindViewById<TextView> (Resource.Id.projectName).Text = currentProject;

  // Add the tile
  projectScrollView.AddView (tile);
}   

// you can get items from your list by using  project.Count, your List<string> instead of projects.Length your array and take information from your list and output your tiles that way

Solution 3

To initialize a List<string> with collection initializer use the below syntax.

List<String> projects = new List<String>(){"hey","yo","app","xamarin","c","xaml"};

Count is not a method it is a property. You need property syntax.

int amount = projects.Count;
Share:
17,962

Related videos on Youtube

Erik
Author by

Erik

Programming ethusiast and motocross rider: I'm on Twitter and I'm on Facebook Developer of the app "Knowledge Organizer" for Windows Phone 8. Link: http://www.windowsphone.com/en-us/store/app/knowledge-organizer/611c36ab-db53-40e2-b1b2-043899bbe6bc pssstt (Feedback on app very much appreciated;)) Love creating stuff both hardware- and software-wise

Updated on September 28, 2022

Comments

  • Erik
    Erik over 1 year

    I'm building an android application where I need to create a simple list of String items, which i will then add a specific control for each item in the list.

    This is the list I want to create:

    List<String> projects = new List<String>(); // How?
    

    The code I was trying:

    String projects = new string[] { "hey","yo","app","xamarin","c","xaml" };
    

    I need to count the items, something like this:

    int amount = projects.Count(); // Can I do this?
    

    Then adding the controls for each item in the list

    // Add the tiles, one by one
    for (int i = 0; i < amount; i++)
    {
      // Inflate the tile
      var tile = LayoutInflater.Inflate (Resource.Layout.Tile, null);
    
      // Set its attributes
      tile.FindViewById<TextView> (Resource.Id.projectName).Text = currentProject;
    
      // Add the tile
      projectScrollView.AddView (tile);
    }
    

    "currentProject" string is retrieved from SharedPreferences, just haven't got that far yet

  • Magus
    Magus about 10 years
    Can't you leave off the new List<String>()? I know the parens at least are extra.
  • Sriram Sakthivel
    Sriram Sakthivel about 10 years
    @Magus yup, that's optional. but I believe it will create an array not list. Except parenthesis rest is required
  • Magus
    Magus about 10 years
    Except, no, you should use .AddRange and avoid the loop and array resizes.
  • Erik
    Erik about 10 years
    But the List<String> isn't detected when I type it. Do I need to add a "using" line?
  • Ronnie
    Ronnie about 10 years
    What are your "using" right now? hard to say if you have it there if it is unknown.
  • Magus
    Magus about 10 years
    List<T> is part of System.Collections.Generic
  • Erik
    Erik about 10 years
    I'm trying to use this code, but "List" isn't found. Just get an error. What import/using is it?
  • Birk
    Birk about 10 years
    System.Collections.Generic;