J'ai un contrôle ListView dans ma disposition d'activité principale. Sur chaque article de ma ListView, j'ai un bouton pour afficher les détails (ouverture d'une nouvelle activité) et afficher une carte (ouvre une application Google Maps externe). Le problème que j'ai est que je voudrais que l'utilisateur puisse cliquer sur l'élément ListView lui-même (pas un bouton) afin que la carte (à l'intérieur du fragment qui est au-dessus de la ListView) puisse être mise à jour. Pour une raison quelconque, mon événement ListView.itemclick ne s'exécute pas lorsque je clique sur un élément ListView. Dois-je ajouter quelque chose à mon code pour que cela fonctionne? J'ai parcouru le web et trouvé plusieurs suggestions pour corriger cela, mais rien ne fonctionne.ListView ItemClick ne répond pas
Activité principale mise en page
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="3"
android:columnCount="1">
<LinearLayout
android:id="@+id/linearLayout1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"
android:layout_width="match_parent" />
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="300dp"
class="com.google.android.gms.maps.MapFragment" />
<ListView
android:id="@+id/List"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:cacheColorHint="#FFDAFF7F" android:focusable="false" />
</LinearLayout>
Code principal Activité:
protected override void OnCreate(Bundle bundle)
{
try
{
// instantiate markList
this.markerList = new List<Marker>();
base.OnCreate(bundle);
this.SetContentView(Resource.Layout.Main);
// set toolbar
var toolbar = this.FindViewById<Toolbar>(Resource.Id.toolbar);
this.SetActionBar(toolbar);
this.ActionBar.Title = "Customer Map";
// setup map
this.SetUpMap();
// get the current location
this.locationManager = (LocationManager)this.GetSystemService(LocationService);
var criteria = new Criteria { Accuracy = Accuracy.Coarse, PowerRequirement = Power.Medium };
this.provider = this.locationManager.GetBestProvider(criteria, true);
this.currentLocation = this.locationManager.GetLastKnownLocation(this.provider);
this.geoCoder = new Geocoder(this);
// set up background color
this.SetBackgroundColorOfLinearLayout();
// setup the list view
this.listView = this.FindViewById<ListView>(Resource.Id.List);
this.listView.NestedScrollingEnabled = true;
var customer = new CustomerService();
this.tableItems = customer.GetCustomerInfo("104", this.currentLocation.Latitude.ToString(), this.currentLocation.Longitude.ToString());
this.listView.ItemClick += this.ListView_ItemClick;
this.listView.Adapter = new ListOfLocationAdapter(this, this.tableItems);
}
catch (Exception ex)
{
var errorMessage = "An error occurred during processing the main activity";
if (ex.InnerException != null && ex.InnerException.ToString() != string.Empty)
{
errorMessage = ex.InnerException.ToString();
}
var errorOccurred = new Intent(this, typeof(ErrorOccurred));
errorOccurred.PutExtra("ErrorMessage", errorMessage);
this.StartActivity(errorOccurred);
}
}
private void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
var item = this.tableItems.ElementAt(e.Position);
var item1 = item.FullName;
Toast.MakeText(this, "delegate", ToastLength.Long).Show();
}
Liste des emplacements Mise en page
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="70dp"
android:orientation="vertical"
android:background="#ffffff">
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rowCount="1"
android:columnCount="2">
<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/CustomerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="20dip"
android:textStyle="italic"
android:paddingLeft="10dip" />
<TextView
android:id="@+id/Address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textColor="#000000"
android:paddingLeft="10dip" />
<TextView
android:id="@+id/Distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textColor="#000000"
android:paddingLeft="10dip" />
<TextView
android:id="@+id/CustomerNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textColor="#000000"
android:paddingLeft="10dip"
android:visibility="invisible" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout2"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:text="Details"
android:layout_width="60dp"
android:layout_height="38dp"
android:id="@+id/buttonDetails"
android:textSize="9dp"
android:layout_alignParentRight="true" />
<Button
android:text="Directions"
android:layout_width="80dp"
android:layout_height="38dp"
android:id="@+id/buttonOpenMap"
android:textSize="9dp"
android:layout_alignParentRight="true" />
</LinearLayout>
</GridLayout>
ListOfLocations Adaptateur
public override View GetView(int position, View convertView, ViewGroup parent)
{
this.view = convertView ?? this.context.LayoutInflater.Inflate(Resource.Layout.ListOfLocations, null);
// fill values into textview
var item = this.items[position];
this.view.FindViewById<TextView>(Resource.Id.CustomerName).Text = item.FullName;
this.view.FindViewById<TextView>(Resource.Id.Address).Text = this.ReturnFullAddress(item);
this.view.FindViewById<TextView>(Resource.Id.Distance).Text = this.FormatDistance(item.ApproxDistance);
this.view.FindViewById<TextView>(Resource.Id.CustomerNumber).Text = item.CustomerNumber;
// add click event to open map button
this.view.FindViewById<Button>(Resource.Id.buttonOpenMap).Click += delegate
{
var geoURL = "geo:0,0?q=" + this.ReturnFullAddress(item) + "&z=20";
var geoUri = Uri.Parse(geoURL);
var mapIntent = new Intent(Intent.ActionView, geoUri);
this.context.StartActivity(mapIntent);
};
// add click event to details button
this.view.FindViewById<Button>(Resource.Id.buttonDetails).Click += delegate
{
var activity = new Intent(this.context, typeof(CustomerDetail));
activity.PutExtra("CustomerNumber", item.CustomerNumber);
this.context.StartActivity(activity);
};
return this.view;
}