如何使用列表<;哈希表>;来自c#
如何在C#中存储和检索列表中的值?我可以像这样存储值如何使用列表<;哈希表>;来自c#,c#,c#-4.0,xamarin,xamarin.android,xamarin.forms,C#,C# 4.0,Xamarin,Xamarin.android,Xamarin.forms,如何在C#中存储和检索列表中的值?我可以像这样存储值 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); List mList=new List(); Hashtable ht=新的Hasht
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);
List mList=new List();
Hashtable ht=新的Hashtable();
ht.添加(1,“一”);
ht.添加(2,“两”);
ht.添加(3,“三”);
ht.添加(4,“四”);
列表添加(ht);
如何将这些值设置为C#中的基本适配器文本视图
如何将这些值设置为C#中的基本适配器文本视图
我假设您的基本适配器是指列表视图的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;
}
}
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);
}
}
List<string> mList = new List<string>();
mList.Add("One");
mList.Add("Two");
mList.Add("Three");
mList.Add("Four");
var index = mList.IndexOf("Three");
List
本身为每个项目分配一个索引,例如,如果您想找到字符串项目“三”的索引,您可以这样编码:
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;
}
}
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);
}
}
List<string> mList = new List<string>();
mList.Add("One");
mList.Add("Two");
mList.Add("Three");
mList.Add("Four");
var index = mList.IndexOf("Three");
由于列表中的第一个项目将与索引0匹配,因此项目“三”的索引将为2
当然,我并不是说不允许将哈希表
存储到列表
中,但通常当我们想要为每个项定义键时,一种方法是创建类模型,例如:
public class MyListModel
{
public int Key { get; set; }
public string Content { get; set; }
}
现在,您可以为该模型创建一个列表
:
List<MyListModel> mList = new List<MyListModel>();
for (int i = 1; i <= 10; i++)
{
mList.Add(new MyListModel { Key = i, Content = "item " + i });
}
List mList=new List();
对于(int i=1;我不知道Xamarin textview,但是在列表中放置一个旧式哈希表看起来很奇怪。你确定需要这个吗?谢谢,伙计。是的,我也需要并告诉我旧式。我想使用key存储多个值