I have a problem I hope you can help me with!
I have an activity where I have a recycler view
with some items and I use the google fused location API to get the distance from the user location to the items in the recycler view
. I have made a click event for the recycler view
and tested that the code for the recycler view
and click event works. But when I add the functionality from the location API the click event stops working. It will not fire the event, even though it makes a click sound when I click on one of the items on the phone, the OnItemClick
method is never called. The location API updates the user's location every 20 meters and uses an async method for this. When I click on one of the items this comes in the output log:
03-21 18:06:19.163 V/BoostFramework(22705): BoostFramework() : mPerf = com.qualcomm.qti.Performance@4b3d5f9
Does anyone know what I can do to make the click event work again? Thanks!
Here is the code for the location updates in the main activity:
[Activity (Label = "Events", MainLauncher = false, Icon = "@drawable/icon")]
public class MainActivity : AppCompatActivity, GoogleApiClient.IConnectionCallbacks,
GoogleApiClient.IOnConnectionFailedListener, Android.Gms.Location.ILocationListener
{
GoogleApiClient apiClient;
Android.Locations.Location location;
int locationRequestCode = 1;
RecyclerView rv;
EventAdapter adapter;
List<Event> allEvents;
List<Event> eventsNearby;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
var name = FindViewById<TextView>(Resource.Id.eventName);
var date = FindViewById<TextView>(Resource.Id.eventDate);
var desc = FindViewById<TextView>(Resource.Id.eventDesc);
var city = FindViewById<TextView>(Resource.Id.eventCity);
var webhandle = new utils.Webhandler();
BuildAndConnectApi();
rv = FindViewById<RecyclerView>(Resource.Id.recyclerView);
rv.SetLayoutManager(new LinearLayoutManager(this));
rv.SetItemAnimator(new DefaultItemAnimator());
eventsNearby = new List<Event>();
allEvents = new List<Event>();
var json = JsonConvert.SerializeObject(Event.GetEvents());
allEvents = JsonConvert.DeserializeObject<List<Event>>(json);
adapter = new EventAdapter(eventsNearby);
rv.SetAdapter(adapter);
adapter.ItemClick += OnItemClick;
}
private void OnItemClick(object sender, int position)
{
//Log.Info("Click Event:", allEvents[position].name);
Toast.MakeText(this, "Item clicked!", ToastLength.Short).Show();
}
public void BuildAndConnectApi()
{
apiClient = new GoogleApiClient.Builder(this, this, this)
.AddApi(LocationServices.API).Build();
apiClient.Connect();
}
public async void RequestLocationAsync()
{
if (apiClient.IsConnected)
{
Log.Info("Location:", "Requesting Location Updates");
var locationRequest = new LocationRequest();
locationRequest.SetPriority(100);
locationRequest.SetSmallestDisplacement(20);
await LocationServices.FusedLocationApi.RequestLocationUpdates(apiClient, locationRequest, this);
}
else
{
Log.Info("Location:", "Client API not connected");
}
}
public void OnConnected(Bundle connectionHint)
{
Log.Info("Location:", "Connected!");
location = LocationServices.FusedLocationApi.GetLastLocation(apiClient);
DisplayLocation(location);
CheckLocationPermission();
RequestLocationAsync();
}
public void OnConnectionSuspended(int cause)
{
throw new System.NotImplementedException();
}
public void OnConnectionFailed(Android.Gms.Common.ConnectionResult result)
{
Log.Info("Location:", "Connected failed!");
//Log.Info("FEIL:", "Connection failed: ConnectionResult.getErrorCode()" + result.ErrorCode);
RequestLocationAsync();
}
public void OnLocationChanged(Android.Locations.Location location)
{
Log.Info("Location:", "Location updates:\n");
DisplayLocation(location);
//GetLocationUpdates();
}
private void DisplayLocation(Android.Locations.Location location)
{
if (location != null)
{
Log.Info("Location:", "Latitude: " + location.Latitude.ToString() + "\n" + "Longitude: " + location.Longitude.ToString() + "\n");
}
else
{
Log.Info("Location:", "No location info available");
}
}