Creating a List<String> in Android Xamarin
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;
Related videos on Youtube
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, 2022Comments
-
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 about 10 yearsCan't you leave off the
new List<String>()
? I know the parens at least are extra. -
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 about 10 yearsExcept, no, you should use
.AddRange
and avoid the loop and array resizes. -
Erik about 10 yearsBut the List<String> isn't detected when I type it. Do I need to add a "using" line?
-
Ronnie about 10 yearsWhat are your "using" right now? hard to say if you have it there if it is unknown.
-
Magus about 10 years
List<T>
is part ofSystem.Collections.Generic
-
Erik about 10 yearsI'm trying to use this code, but "List" isn't found. Just get an error. What import/using is it?
-
Birk about 10 yearsSystem.Collections.Generic;