Android:无法创建泛型类

时间:2013-07-22 10:04:38

标签: java android generics azure azure-storage

我之前没有和Generics合作过。现在是时候了,当我应该创建一个泛型类时,否则我的代码长度会变得更大,更难以理解。

这是将记录插入位于Windows Azure的数据库的方式:

public class Item {
    public int Id;
    public String Text;
}

在您定义mClient的同一活动中,添加以下代码:

Item item = new Item();
item.Text = "Awesome item";
mClient.getTable(Item.class).insert(
        item, 
        new TableOperationCallback<Item>() { 
            public void onCompleted(
                    Item entity,
                    Exception exception, 
                    ServiceFilterResponse response
            ) { 
                if (exception == null) { 
                     // Insert succeeded 
                } else { 
                    // Insert failed 
                } 
            } 
        });

我无法为在Windows Azure上进行插入,删除等数据库操作创建泛型类。

这是windows azure引用的链接(如果需要):

http://dl.windowsazure.com/androiddocs/

我尝试了以下代码:

public class WindowsAzureOperations<T> {

    T mT;
    MobileServiceClient mClient;
    Context mContext;

    public void insert(MobileServiceClient mClient, T tObject, final Context tmpContext) {

        this.mClient = mClient;
        mT = tObject;
        mContext = tmpContext;

        this.mClient.getTable(mT.getClass()).insert(mT,  // error in this line
                new TableOperationCallback<T>() {

                    public void onCompleted(T entity, Exception exception,
                            ServiceFilterResponse response) {

                        if (exception == null) {

                        } else {

                        }
                    }
                });
    }
}

显示以下错误:

  

insert(capture#1-of ? extends Object, TableOperationCallback<capture#1-of ? extends Object>)类型中的方法MobileServiceTable<capture#1-of ? extends Object>不适用于参数(T, new TableOperationCallback<T>(){})

请帮帮我。提前谢谢。

1 个答案:

答案 0 :(得分:2)

问题在于你如何调用getTable。给定Class<T>,该方法将返回MobileServiceTable<T>。但是mT.getClass()会返回Class<? extends T>,因为mT的运行时类型可能是T 或某种类型的 T

要解决此问题,请让您的insert方法采用与Class<T>类型完全相同的参数:

public void insert(
        MobileServiceClient mClient,
        T tObject,
        Class<T> objectType,
        final Context tmpContext
) {

    this.mClient = mClient;
    mT = tObject;
    mContext = tmpContext;

    this.mClient.getTable(objectType).insert(
            mT,
            new TableOperationCallback<T>() {

                public void onCompleted(T entity, Exception exception,
                        ServiceFilterResponse response) {

                    if (exception == null) {

                    } else {

                    }
                }
            });
}

我还建议您将mClientmTmContext的分配移至WindowsAzureOperations的构造函数。实际上,以表面值为基础,没有必要实例化它 - 只需使用静态泛型方法:

public final class WindowsAzureOperations {

    // now a utility class so prevent instantiation
    private WindowsAzureOperations() { }

    public static <T> void insert(
            MobileServiceClient mClient,
            T tObject,
            Class<T> objectType,
            final Context tmpContext
    ) {
        mClient.getTable(objectType).insert(
                tObject,
                new TableOperationCallback<T>() {
                    @Override
                    public void onCompleted(
                            T entity,
                            Exception exception,
                            ServiceFilterResponse response
                    ) {
                        if (exception == null) {
                            //...
                        }
                        else {
                            //...
                        }
                    }
                }
        );
    }
}