Android的:在一个ContentProvider的调用自定义方法 [英] Android: call custom methods in a ContentProvider
问题描述
我有一个自定义的ContentProvider我用它来存储从GPS获得的成SQLite数据库修复。这个类覆盖ContentProvider的方法(删除,插入,查询...),但是请注意我还包括一个查询的最后一个修复方法。
公共类FixesContentProvider扩展ContentProvider的{
@覆盖
公众诠释删除(...){...}
...
@覆盖
公共光标查询(...){...}
公共光标getLastFix(URI URI){...}
}
这是pretty的方便调用查询到其他服务或活动的数据 getContentResolver()
。
ContentResolver的CR = getContentResolver();
光标修复= cR.query(CONTENT_URI,...);
但我不能叫我的自定义方法( FixesContentProvider#getLastFix
)。 我怎么能调用ContentProvider的定义自定义方法?
也许我不明白ContentProviders / ContentResolvers在这种情况下是如何工作的,但官方的Android文档所提供的谷歌是不是在这个意义上非常明确的。
但我不能叫我的自定义方法(
FixesContentProvider#getLastFix
)。
这是完全正确的。在这种非常情况下,虽然,你可以利用一个事实优势:无论查询
和 getLastFix
方法返回一个光标
对象。所以,我做什么会加入一些价值的URI传递给查询
和,然后再决定你想要什么真正做到:查询
或 getLastFix
。例如:
公开光标查询(URI URI,...){
如果(URI表示,它已经返回的最后一个补丁)
返回getLastFix(URI);
//否则,执行正常操作...
返回游标;
}
其实,查询
方法是假设返回结果,给予乌里
。所以,如果你需要找回过去的修补程序,您必须指定所需的最后一个补丁的URI。
I have a custom ContentProvider I use to store fixes obtained from the GPS into a SQLite database. This class overrides ContentProvider methods (delete, insert, query...), but notice I have also included a method to query the last fix.
public class FixesContentProvider extends ContentProvider {
@Override
public int delete(...) { ... }
...
@Override
public Cursor query(...) { ... }
public Cursor getLastFix(Uri uri) { ... }
}
It is pretty easy to query data from other services or activities by calling getContentResolver()
.
ContentResolver cR = getContentResolver();
Cursor fixes = cR.query(CONTENT_URI, ...);
But I am not able to call my custom method (FixesContentProvider#getLastFix
). How could I call custom methods defined in a ContentProvider?
Maybe I don't understand how ContentProviders/ContentResolvers work in this case, but official Android documentation provided by Google is not very clear in this sense.
But I am not able to call my custom method (
FixesContentProvider#getLastFix
).
That's completely true. In this very case though, you can take advantage of one fact: both query
and getLastFix
methods return a Cursor
object. So, what I do would be adding some value to the URI that you pass to query
and, then decide what you want to actually do: query
or getLastFix
. For instance:
public Cursor query(Uri uri,...) {
if ( uri says it has to return the last fix )
return getLastFix(uri);
// otherwise, do normal operations...
return cursor;
}
In fact, the query
method is suppose to return a result, given a Uri
. So, if you need to retrieve the last fix, you have to specify in the URI that you want the last fix.
这篇关于Android的:在一个ContentProvider的调用自定义方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!