Reputation: 37
How to store and retrieve values from List in C#? I can store values like this
List<Hashtable> mList=new List<Hashtable>();
Hashtable ht = new Hashtable();
ht.Add(1, "One");
ht.Add(2, "Two");
ht.Add(3, "Three");
ht.Add(4, "Four");
mList.Add(ht);
How to set these values to base adapter textview in C#?
Upvotes: 0
Views: 1988
Reputation: 16652
How to set these values to base adapter textview in C#?
I assume that your base adapter means the BaseAdapter
for a ListView
, then you can create a adapter inherit from BaseAdapter
for example like this:
public class MainAdapter : BaseAdapter<Hashtable>
{
private List<Hashtable> items;
private Activity context;
public MainAdapter(Activity context, List<Hashtable> items) : base()
{
this.context = context;
this.items = items;
}
public override Hashtable this[int position]
{
get
{
return items[position];
}
}
public override int Count
{
get
{
return items.Count;
}
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem1, null);
view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = items[position].ToString();
return view;
}
}
and use the List<HashTable>
for your adapter like this:
public class MainActivity : ListActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
//SetContentView(Resource.Layout.Main);
List<Hashtable> mList = new List<Hashtable>();
Hashtable ht = new Hashtable();
ht.Put(1, "One");
ht.Put(2, "Two");
ht.Put(3, "Three");
ht.Put(4, "Four");
mList.Add(ht);
ListAdapter = new MainAdapter(this, mList);
}
}
But I doubt this is really what you need. You have only one item in List<>
and this item has one Hashtable
which contains four key-pair value. Is it possible that you need a key-pair value to be shown on each item of ListView
instead of showing four key-pair values in one item?
I do agree with @Henk Holterman, it is very strange to put an old-style hashtable in a List<>
, if you want to use List<>
to store multiple string values with key, you can simply code like this:
List<string> mList = new List<string>();
mList.Add("One");
mList.Add("Two");
mList.Add("Three");
mList.Add("Four");
List<>
itself allocates a index for each item, for example, if you want to find the index of string item "Three", you can code like this:
var index = mList.IndexOf("Three");
Since the first item in List<>
will match the index of 0, here the index of item "Three" will be 2.
Of course I'm not saying it is not allowed to store a Hashtable
into a List<>
, but usually when we want to define a Key for each item, one method is to create a class model, for example:
public class MyListModel
{
public int Key { get; set; }
public string Content { get; set; }
}
And now you can create a List<>
for this model:
List<MyListModel> mList = new List<MyListModel>();
for (int i = 1; i <= 10; i++)
{
mList.Add(new MyListModel { Key = i, Content = "item " + i });
}
The advantage to use a data model for ListView
is that if your each ListView
item has for example more than one TextView
, each is for displaying different information, then you can simply add those properties in this class model.
For more information of building a ListView
in Xamarin.Android, you can refer to ListViews and Adapters.
Upvotes: 1