java - UriMatcher doesn't match correctly -
first up: have sifted through many of questions on topic on , have still failed come correct answer.
here a, (way simplified), version of code:
private static urimatcher urimatcher = new urimatcher(urimatcher.no_match); private static final int homework_table_request = 1, class_table_request = 2, settings_table_request = 3, homework_item_request = 4, class_item_request = 5, settings_item_request = 6; static { urimatcher.adduri("org.dvc.homeworkreminder.homeworkprovider", "homework", homework_table_request); urimatcher.adduri("org.dvc.homeworkreminder.homeworkprovider", "homework/#", homework_item_request); urimatcher.adduri("org.dvc.homeworkreminder.homeworkprovider", "class", class_table_request); urimatcher.adduri("org.dvc.homeworkreminder.homeworkprovider", "class/#", class_item_request); urimatcher.adduri("org.dvc.homeworkreminder.homeworkprovider", "settings", settings_table_request); urimatcher.adduri("org.dvc.homeworkreminder.homeworkprovider", "settings/#", settings_item_request); }
and here query method:
@override public cursor query(uri uri, string[] projection, string selection, string[] selectionargs, string sortorder) { cursor cursor = null; log.i("dfg", "query method called"); log.i("dfg", "uri = " + uri.tostring()); log.i("dfg", "match: " + urimatcher.match(uri)); switch(urimatcher.match(uri)) { case homework_item_request: log.i("dfg", "homework_item_request"); cursor = database.readcursorbyid(integer.parseint(uri.getlastpathsegment())); break; case homework_table_request: log.i("dfg", "homework_table_request"); cursor = database.readallhomework(); break; case class_item_request: case class_table_request: case settings_item_request: case settings_table_request: cursor = null; break; default: cursor = null; break; } return cursor; }
i have no implementation class requests or settings requests that's why i'm defaulting return null
. what's happening switch
statement falling way through default:
, , causing npe's galore later on in code. you'll notice there 5 log
statements in code. following printed logcat. (why called logcat anyways?)
query method called uri = content://org.dvc.homeworkreminder.homeworkprovider/homework/24 match: -1
now uri being tested should match 2nd pattern added, correct? read how uri.parse()
method messes urimatcher
's wildcards on thread build above-printed uri
following code:
uri returnuri = new uri.builder().scheme(contentresolver.scheme_content).authority("org.dvc.homeworkreminder.homeworkprovider").appendpath("homework").appendpath(string.valueof(id)).build();
the id
variable there depends on other stuff isn't relevant.
my question why urimatcher
not working, , how go fixing it?
fix:
i had incorrect capitalization on following lines:
urimatcher.adduri("org.dvc.homeworkreminder.homeworkprovider", "homework/#", homework_item_request); urimatcher.adduri("org.dvc.homeworkreminder.homeworkprovider", "class/#", class_item_request); urimatcher.adduri("org.dvc.homeworkreminder.homeworkprovider", "settings/#", settings_item_request);
notice lowercase r
in homeworkreminder
. bruce spotting it!
you're using lower-case 'reminder' when set matcher, actual uri has upper-case 'reminder'.
Comments
Post a Comment