我的代码记录了当前的经度/纬度并放入一个数组,您可以在GPSHistory活动中查看这些记录的所有历史记录。
btnGPSHistory = FindViewById<Button>(Resource.Id.btnGPSHistory);
btnGPSHistory.Click += (sender, e) =>
{
var intent = new Intent(this, typeof(GPSHistory));
intent.PutStringArrayListExtra("Answer", liGPSAnswer);
StartActivity(intent);
};
这会将双精度型转换为字符串,然后放入字符串并将其发送到数组。
async private void GetGPS(object sender, EventArgs e)
{
if (here == null)
{
txtGPSAnswer.Text = "Try Again Later.";
return;
}
dblLongitude = here.Longitude;
dblLatitude = here.Latitude;
strLongitude = dblLongitude.ToString("G");
strLatitude = dblLatitude.ToString("G");
strGPSAnswer = "Longitude: " + strLongitude + " Latitude: " + strLatitude;
if (string.IsNullOrEmpty(strGPSAnswer))
{
txtGPSAnswer.Text = "";
}
else
{
txtGPSAnswer.Text = strGPSAnswer;
liGPSAnswer.Add(strGPSAnswer);
btnGPSHistory.Enabled = true;
}
}
这是来自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);
}
}
}
该代码记录经度和纬度细度,并将其放入历史记录页面而没有任何问题。我的问题是如何在数组中的每个项目上附加一个按钮,该按钮基本上告诉程序将这些坐标设为“当前坐标”,以便程序可以在地图上查找这些坐标。谢谢。
答案 0 :(得分:1)
您是说要在ListView的项目中添加Button吗?如果是,则应使用自定义适配器,如下所示:
1。创建您 CustomAdapter :
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];
}
}
}
item_listview
axml是您的自定义布局:
<?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>
2。然后您可以在活动中进行如下更改:
this.ListAdapter = new CustomAdapter(strLongitude);