[英]How to use List<Hashtable> from c#
How to store and retrieve values from List in C#? 如何在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#? 如何在C#中将这些值设置为基本适配器textview?
How to set these values to base adapter textview in C#?
如何在C#中将这些值设置为基本适配器textview?
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: 我假设您的基本适配器意味着
ListView
的BaseAdapter
,那么您可以创建一个继承自BaseAdapter
的适配器,例如:
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: 并使用
List<HashTable>
作为适配器,如下所示:
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. List<>
只有一个项目,并且此项目具有一个Hashtable
,该Hashtable
包含四个密钥对值。 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? 是否有可能需要在
ListView
每个项目上显示一个密钥对值,而不是在一个项目中显示四个密钥对值?
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: 我确实同意@Henk Holterman,将旧式哈希表放在
List<>
是很奇怪的,如果要使用List<>
存储带有键的多个字符串值,则可以像这样简单地编码:
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: List<>
本身为每个项目分配一个索引,例如,如果要查找字符串项目“ Three”的索引,则可以这样编写:
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. 由于
List<>
的第一个项目将与索引0匹配,因此项目“三”的索引将为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: 当然,我并不是说不允许将
Hashtable
存储到List<>
,但是通常当我们想为每个项目定义Key时,一种方法是创建一个类模型,例如:
public class MyListModel
{
public int Key { get; set; }
public string Content { get; set; }
}
And now you can create a List<>
for this model: 现在,您可以为此模型创建一个
List<>
:
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. 将数据模型用于
ListView
的好处是,如果您的每个ListView
项例如具有多个TextView
,则每个TextView
都用于显示不同的信息,那么您可以在此类模型中简单地添加这些属性。
For more information of building a ListView
in Xamarin.Android, you can refer to ListViews and Adapters . 有关在Xamarin.Android中构建
ListView
更多信息,可以参考ListViews和Adapters 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.