I'm attempting to create a function where I can pass names and values that will be put into a ContentValues. The problem i'm having is that I'm trying to allow passing more that one key/value pair at a time, so keys can be an array of strings, but values have to be objects and ContentValues.put() doesn't allow Objects, they need to be cast to String, Double, Float, etc. Is there a way I can determine the type of the object and cast it so it will call the correct put()? Below is one method that does work, but it requires me to add an if for every possible type of value.
public long create(String[] names, Object[] values) {
ContentValues initialValues = new ContentValues();
for (int i = 0; i < names.length; i++)
{
String type = values[i].getClass().getName();
if (type.equals("java.lang.Double"))
{
initialValues.put(names[i], (Double)values[i]);
}
else if (type.equals("java.lang.String")) {
initialValues.put(names[i], (String)values[i]);
}
else
{
throw new InvalidParameterException("Unable to convert type:"+type);
}
}
return mDb.insert(this.getTableName(), null, initialValues);
}