我以前没有使用过泛型。现在是我应该创建一个泛型类的时候了,否则我的代码长度会变得更大更难理解。
这是将记录插入到位于 Windows Azure 的数据库的方式:
public class Item {
public int Id;
public String Text;
}
在定义移动客户端的同一活动中,添加以下代码:
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>(){})
请帮我。提前致谢。