C# 向数组中的每个项添加按钮
我的代码记录当前的经度/纬度,并放入一个数组中,您可以在GPS存储活动中查看这些记录的所有历史记录C# 向数组中的每个项添加按钮,c#,android,xamarin,android-5.0-lollipop,C#,Android,Xamarin,Android 5.0 Lollipop,我的代码记录当前的经度/纬度,并放入一个数组中,您可以在GPS存储活动中查看这些记录的所有历史记录 btnGPSHistory = FindViewById<Button>(Resource.Id.btnGPSHistory); btnGPSHistory.Click += (sender, e) => { var intent = new Intent(this, typeof(GPSHistory));
btnGPSHistory = FindViewById<Button>(Resource.Id.btnGPSHistory);
btnGPSHistory.Click += (sender, e) =>
{
var intent = new Intent(this, typeof(GPSHistory));
intent.PutStringArrayListExtra("Answer", liGPSAnswer);
StartActivity(intent);
};
这是来自GPSHistory.cs的
namespace GPS2
{
[Activity(Label = "@string/GPSHistory")]
public class GPSHistory : ListActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Create your application here
var strLongitude = Intent.Extras.GetStringArrayList("Answer") ?? new string[0];
this.ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, strLongitude);
}
}
}
名称空间GPS2
{
[活动(标签=“@string/GPSHistory”)]
公共类GPSHistory:ListActivity
{
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
//在此处创建应用程序
var strlongite=Intent.Extras.GetStringArrayList(“应答”)??新字符串[0];
this.ListAdapter=newarrayadapter(this,Android.Resource.Layout.SimpleListItem1,strlongite);
}
}
}
代码可以很好地记录经度和纬度,并且可以毫无问题地放入历史页面。我的问题是如何在数组中的每个项目上附加一个按钮,该按钮基本上告诉程序将这些坐标设置为“当前坐标”,以便程序可以在地图上查看这些坐标。谢谢。您的意思是要将按钮添加到ListView的项目中吗?如果是,您应该像这样使用自定义适配器: 1.创建您的自定义适配器:
class CustomAdapter : BaseAdapter, View.IOnClickListener
{
private Dictionary<int, string> dictionary = new Dictionary<int, string>();
List<string> items; //this is the data in my code ,you should replace your own data
public CustomAdapter(List<string> value) // the parameter use your own data
{
//copy your data into the dictionary
items = new List<string>();
items = value;
for (int i = 0; i < items.Count; i++)
{
dictionary.Add(i, items[i].ToString());
}
}
public override Object GetItem(int position)
{
return items[position];
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
View view = convertView;
if (view == null) // no view to re-use, create new
view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.item_listview, null);
view.FindViewById<TextView>(Resource.Id.txt_location).Text = item;
var button1 = view.FindViewById<Button>(Resource.Id.btn_set);
button1.Tag = position;
button1.SetOnClickListener(this);
return convertView;
}
public override int Count { get; }
public void OnClick(View v)
{
//do the thing you want,location is your strLongitude
var location = dictionary[(int) v.Tag];
}
}
二,。然后在你的活动中,你可以这样改变:
this.ListAdapter = new CustomAdapter(strLongitude);
变量
liGPSAnswer
是否表示您的数组?@leon是的,确实如此。我应该提到它是这样做的:static readonly List liGPSAnswer=new List()代码>好的,在这种情况下,您可能需要这样一个数组:列表
,然后您可以添加字符串和相应的按钮。使用异步任务
总是比使用异步无效
更好。我不知道两者之间的区别。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id = "@+id/txt_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id = "@+id/btn_set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current Ones"
/>
</LinearLayout>
this.ListAdapter = new CustomAdapter(strLongitude);