2017-10-10 53 views
0

我有我的主要活動佈局內的ListView控件。在ListView中的每個項目上,我都有一個用於查看詳細信息(打開新活動)和查看地圖(打開外部谷歌地圖應用程序)的按鈕。我遇到的問題是我希望用戶能夠點擊ListView項目本身(而不是按鈕),以便可以更新地圖(位於ListView上方的片段內)。出於某種原因,當我點擊ListView項目時,我的ListView.itemclick事件不會執行。我需要添加一些東西到我的代碼才能使這個工作?我搜索了網頁,發現了一些修復此問題的建議,但沒有任何效果。ListView ItemClick沒有響應

主要活動佈局

<?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> 

主要活動代碼:位置的

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(); 
    } 

列表佈局

<?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適配器

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; 
    } 

回答