问题
I am applying Google Places Api on my app and converting the Java to C# code. But when I am getting the nearby places in the list, I got this error:
Cannot implicitly convert type 'Java.Lang.Object' to 'Android.Runtime.JavaDictionary'. An explicit conversion exists (are you missing a cast?)
private void showNearbyPlaces(JavaList<JavaDictionary<string, string>> placeList)
{
for (int eachPlace = 0; eachPlace < placeList.Size(); eachPlace++)
{
MarkerOptions markerOptions = new MarkerOptions();
JavaDictionary<string, string> googlePlace = placeList.Get(eachPlace); /*This is the error*/
}
}
Screenshot of the code
Any know how to fix this? Thank you.
回答1:
You could use System.Collections.Generic.List instead of JavaList, for example :
using System.Linq;
...
private void showNearbyPlaces(List<JavaDictionary<string, string>> placeList)
{
for (int eachPlace = 0; eachPlace < placeList.Count; eachPlace++)
{
JavaDictionary<string, string> googlePlace = placeList.ElementAt(eachPlace);
}
}
回答2:
Try explicitly casting it:
JavaDictionary<string, string> googlePlace = (type)placeList.Get(eachPlace)
If that doesn't work, I'd suggest converting the placeList to a more primitive object (strings most likely), and then reconstructing it as a dictionary:
JavaDictionary<string, string> googlePlace = placeList.Get(eachPlace).ConvertToString();
public JavaDictionary<string, string> ConvertToString(this [placeListType] placeList)
{
// Create a dictionary
// foreach object in placeList
// Get the first string and assign it
// Get the second string and assign it
// Create a new object with them as parameters
// Add object to dictionary
// Return dictionary
}
回答3:
Use C# IDictionary instead like this,
IDictionary<string, string> placeList= new Dictionary<string, string>();
placeList.Add(new KeyValuePair<string, string>("your string", "your new string"));
来源:https://stackoverflow.com/questions/48161076/cannot-implicitly-convert-type-java-lang-object-to-android-runtime-javadictio