Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a List<String> in Android Xamarin

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

like image 511
Erik Avatar asked Dec 25 '22 13:12

Erik


2 Answers

   var projects = new List<String>() { "hey","yo","app","xamarin","c","xaml" };
like image 114
Jorgesys Avatar answered Jan 06 '23 11:01

Jorgesys


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
like image 38
Ronnie Avatar answered Jan 06 '23 11:01

Ronnie