Android: call custom methods in a ContentProvider

后端 未结 2 529
伪装坚强ぢ
伪装坚强ぢ 2020-12-10 12:47

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...),

2条回答
  •  天命终不由人
    2020-12-10 13:28

    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.

提交回复
热议问题