我正在开发一个程序,我想显示一个包含2行(一个标题和一个文本)的ListView,但这些行来自2个不同的字符串[]
我该怎么做?这是我现在的代码(我只有一行)
public class MainActivity : Activity
{
Button addATaskButton;
ListView listeView;
View view;
Activity context;
private List<string> titres = new List<string>();
private List<string> textes = new List<string>();
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
addATaskButton = FindViewById<Button>(Resource.Id.AddATaskButton);
listeView = FindViewById<ListView>(Resource.Id.listeTasks);
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(ApplicationContext);
ISet<string> listeTitresTaches = new HashSet<string>(prefs.GetStringSet("Titres", new HashSet<string>()));
ISet<string> listeTextesTaches = new HashSet<string>(prefs.GetStringSet("Textes", new HashSet<string>()));
foreach (string items in listeTitresTaches)
{
titres.Add(items);
}
foreach (string items2 in listeTextesTaches)
{
textes.Add(items2);
}
//view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem2, null);
var ListAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem2, titres.ToArray());
listeView.Adapter = ListAdapter;
addATaskButton.Click += delegate
{
StartActivity(typeof(AddTaskActivity));
};
}
}
感谢您的帮助!
答案 0 :(得分:0)
我想显示一个包含2行的ListView
SimpleAdapter
应该适合你。
请尝试以下代码:
public class MainActivity : Activity
{
ListView lv;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView (Resource.Layout.Main);
lv = FindViewById<ListView>(Resource.Id.listView1);
SimpleAdapter adapter = new SimpleAdapter(this, getData(),Resource.Layout.ListViewItem,
new string[] { "textView1", "textView2" },
new int[] { Resource.Id.textView1, Resource.Id.textView2});
lv.Adapter = adapter;
}
public List<IDictionary<string, object>> getData()
{
List<IDictionary<string, object>> list = new List<IDictionary<string, object>>();
for (int i = 0; i < 10; i++)
{
var item1 = new JavaDictionary<string, object>();
item1.Add("textView1", "Title Mike Ma");
item1.Add("textView2", "Body Mike Ma");
list.Add(item1);
}
return list;
}
}
屏幕截图: