Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查泛型函数android java中的对象类型_Java_Android_Generics - Fatal编程技术网

检查泛型函数android java中的对象类型

检查泛型函数android java中的对象类型,java,android,generics,Java,Android,Generics,我的android应用程序中有一个函数: public static <T> void saveLocalData(Context context, String key, Class<T> value) { // Check type of value here SharedPreferences prefs = context.getSharedPreferences( Constants.PREFERENCES_KEY, Co

我的android应用程序中有一个函数:

public static <T> void saveLocalData(Context context, String key, Class<T> value) {
    // Check type of value here
    SharedPreferences prefs = context.getSharedPreferences(
            Constants.PREFERENCES_KEY, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    if(value.isAssignableFrom(String.class)){
        //Put value here
    }
    else if(value.isAssignableFrom(Boolean.class)){
        //Put value here            
    }

    editor.commit();
}
publicstaticvoidsavelocaldata(上下文、字符串键、类值){
//在此处检查值的类型
SharedReferences prefs=context.getSharedReferences(
常量.PREFERENCES\u KEY,Context.MODE\u PRIVATE);
SharedReferences.Editor=prefs.edit();
if(value.isAssignableFrom(String.class)){
//把价值放在这里
}
else if(value.isAssignableFrom(Boolean.class)){
//把价值放在这里
}
commit();
}
我想检查这个函数中的值的类型(我想检查两种类型
布尔值
字符串
),但我不知道怎么做!有人能给点建议吗?谢谢

编辑:谢谢大家的帮助!我还有一个问题,那就是如何将它的值保存到
首选项

你可以用

确定此类对象表示的类或接口 与的超类或超接口相同,或是的超类或超接口 由指定的类参数表示的类或接口


更新:通过问题中的编辑,为了在a中设置键/值,请使用
putString()
putBoolean()

考虑到您需要接收作为参数的值。请注意,如果您收到该值,您已经可以访问它的类(因此您不需要将
class
作为参数,也不需要
isAssignableFrom()
),并通过运算符检查它:

public static <T> void saveLocalData(Context context, String key, T value) {
    SharedPreferences prefs = context.getSharedPreferences(
        Constants.PREFERENCES_KEY, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    if (value instanceof String) { 
        editor.putString(key, (String) value);
    }
    else if (value instanceof Boolean){
        editor.putBoolean(key, (Boolean) value);
    }
    editor.commit();
}
publicstaticvoidsavelocaldata(上下文、字符串键、T值){
SharedReferences prefs=context.getSharedReferences(
常量.PREFERENCES\u KEY,Context.MODE\u PRIVATE);
SharedReferences.Editor=prefs.edit();
如果(字符串的值实例){
编辑器.putString(键,(字符串)值);
}
else if(布尔值实例){
putBoolean(键,(布尔)值);
}
commit();
}

我还没有在Android上尝试过,但由于它的语法通常与Java相当,所以有两种方法可以在Java中实现:

if (Boolean.class.equals(value) || String.class.equals(value)) {
    // Do stuff.
}
这是因为
Boolean
String
都是最终版本,您不必担心扩展类。我认为您甚至可以在这种情况下使用
=
(假设是一个正常的
类加载器
),因为在类路径上每个
通常只有一个
类的实例。如果您需要担心扩展类,请使用:

if (Boolean.class.isAssignableFrom(value) || String.class.isAssignableFrom(value)) {
    // Do stuff.
}

你能详细解释一下你所说的“检查值类型”是什么意思吗?你能再帮我一个问题吗?谢谢我已经编辑了答案。请注意,如果您已经获得了值(您无论如何都需要在函数中接收该值以设置首选项),则无需传递该值的
。)干得好!非常感谢你!!