Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 790619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:42:42+00:00 2026-05-14T21:42:42+00:00

I’ve built my own content provider and I’ve run into an instance where I

  • 0

I’ve built my own content provider and I’ve run into an instance where I need to execute a query and include a limit param.

The method call to managedQuery doesn’t include this param and there fore I see no way to implement this w/o somehow overriding the managedQuery method?

I’ve created a second query method in my content provider which accepts an additional param, the limit param but I don’t think I can make the managedQuery method call this custom query method of my content provider.

What’s the best way to do this?

For reference, here is my content provider class…
package com.isi.sa;

import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

import com.isi.sa.SATest.Questions;
import com.isi.sa.SATest.Scores;

public class SAContentProvider extends ContentProvider {
  private static final String TAG            = "SAProvider";
    private static final String DATABASE_NAME      = "sa.db";
    private static final int DATABASE_VERSION      = 1;
    private static final String QUESTIONS_TABLE_NAME  = "questions";
    private static final String SCORES_TABLE_NAME    = "scores";

    private static HashMap<String, String> sQuestionsProjectionMap;
    private static HashMap<String, String> sScoresProjectionMap;

    private static final int QUESTIONS = 1;
    private static final int QUESTION_ID = 2;
    private static final int SCORES = 3;
    private static final int SCORE_ID = 4;

    private DatabaseHelper mOpenHelper;
    private static final UriMatcher sUriMatcher;


  @Override
  public boolean onCreate() {
        mOpenHelper = new DatabaseHelper(getContext());
    return true;
  }


  @Override
  public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    switch (sUriMatcher.match(uri)){
    case QUESTIONS:
      qb.setTables(QUESTIONS_TABLE_NAME);
      qb.setProjectionMap(sQuestionsProjectionMap);
      if (TextUtils.isEmpty(sortOrder)) sortOrder = Questions.DEFAULT_SORT_ORDER;
      break;

    case QUESTION_ID:
      qb.setTables(QUESTIONS_TABLE_NAME);
      qb.setProjectionMap(sQuestionsProjectionMap);
      qb.appendWhere(Questions._ID + "=" + uri.getPathSegments().get(1));
      if (TextUtils.isEmpty(sortOrder)) sortOrder = Questions.DEFAULT_SORT_ORDER;
      break;

    case SCORES:
      qb.setTables(SCORES_TABLE_NAME);
      qb.setProjectionMap(sScoresProjectionMap);
      if (TextUtils.isEmpty(sortOrder)) sortOrder = Scores.DEFAULT_SORT_ORDER;
      break;

    case SCORE_ID:
      qb.setTables(SCORES_TABLE_NAME);
      qb.setProjectionMap(sScoresProjectionMap);
      qb.appendWhere(Scores._ID + "=" + uri.getPathSegments().get(1));
      if (TextUtils.isEmpty(sortOrder)) sortOrder = Scores.DEFAULT_SORT_ORDER;
      break;

    default:
      throw new IllegalArgumentException("Unknown URI " + uri);
    }

        // Get the database and run the query
    SQLiteDatabase db = mOpenHelper.getReadableDatabase();
    Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);

        // Tell the cursor what uri to watch, so it knows when its source data changes
    c.setNotificationUri(getContext().getContentResolver(), uri);
    return c;
  }


  public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, String limit) {
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    switch (sUriMatcher.match(uri)){
    case QUESTIONS:
      qb.setTables(QUESTIONS_TABLE_NAME);
      qb.setProjectionMap(sQuestionsProjectionMap);
      if (TextUtils.isEmpty(sortOrder)) sortOrder = Questions.DEFAULT_SORT_ORDER;
      break;

    case QUESTION_ID:
      qb.setTables(QUESTIONS_TABLE_NAME);
      qb.setProjectionMap(sQuestionsProjectionMap);
      qb.appendWhere(Questions._ID + "=" + uri.getPathSegments().get(1));
      if (TextUtils.isEmpty(sortOrder)) sortOrder = Questions.DEFAULT_SORT_ORDER;
      break;

    case SCORES:
      qb.setTables(SCORES_TABLE_NAME);
      qb.setProjectionMap(sScoresProjectionMap);
      if (TextUtils.isEmpty(sortOrder)) sortOrder = Scores.DEFAULT_SORT_ORDER;
      break;

    case SCORE_ID:
      qb.setTables(SCORES_TABLE_NAME);
      qb.setProjectionMap(sScoresProjectionMap);
      qb.appendWhere(Scores._ID + "=" + uri.getPathSegments().get(1));
      if (TextUtils.isEmpty(sortOrder)) sortOrder = Scores.DEFAULT_SORT_ORDER;
      break;

    default:
      throw new IllegalArgumentException("Unknown URI " + uri);
    }

        // Get the database and run the query
    SQLiteDatabase db = mOpenHelper.getReadableDatabase();
    Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder, limit);

        // Tell the cursor what uri to watch, so it knows when its source data changes
    c.setNotificationUri(getContext().getContentResolver(), uri);
    return c;
  }


  @Override
  public Uri insert(Uri uri, ContentValues initialValues) {
        ContentValues values;
        SQLiteDatabase db;
        long rowId;

        if (sUriMatcher.match(uri) != QUESTIONS && sUriMatcher.match(uri) != SCORES) {
            throw new IllegalArgumentException("Unknown URI " + uri);
        }

        if (initialValues != null){
          values = new ContentValues(initialValues);
        }
        else{
          values = new ContentValues();
        }

    switch (sUriMatcher.match(uri)){
    case QUESTIONS:
          if (values.containsKey(Questions.QUESTION) == false) values.put(Questions.QUESTION, "");
        if (values.containsKey(Questions.QUESTION) == false) values.put(Questions.ANSWER, -1);
        if (values.containsKey(Questions.OPTION1) == false) values.put(Questions.OPTION1, "");
        if (values.containsKey(Questions.OPTION2) == false) values.put(Questions.OPTION2, "");
        if (values.containsKey(Questions.OPTION3) == false) values.put(Questions.OPTION3, "");
        if (values.containsKey(Questions.OPTION4) == false) values.put(Questions.OPTION4, "");
        if (values.containsKey(Questions.OPTION5) == false) values.put(Questions.OPTION5, "");
        if (values.containsKey(Questions.OPTION6) == false) values.put(Questions.OPTION6, "");
        if (values.containsKey(Questions.OPTION7) == false) values.put(Questions.OPTION7, "");
        if (values.containsKey(Questions.OPTION8) == false) values.put(Questions.OPTION8, "");
        if (values.containsKey(Questions.OPTION9) == false) values.put(Questions.OPTION9, "");
        if (values.containsKey(Questions.OPTION10) == false) values.put(Questions.OPTION10, "");

          if (values.get(Questions.QUESTION).toString().length() > 0 &&
            values.get(Questions.ANSWER).toString() != "-1" &&
            values.get(Questions.OPTION1).toString().length() > 0){
            db = mOpenHelper.getWritableDatabase();
            rowId = db.insert(QUESTIONS_TABLE_NAME, Questions.QUESTION, values);
            if (rowId > 0) {
                Uri noteUri = ContentUris.withAppendedId(Questions.CONTENT_URI, rowId);
                getContext().getContentResolver().notifyChange(noteUri, null);
                return noteUri;
            }
          }
          break;

    case SCORES:
        if (values.containsKey(Scores.SCORE) == false) values.put(Scores.SCORE, -1);
        if (values.containsKey(Scores.DATE_CREATED) == false) values.put(Scores.DATE_CREATED, DateFormat.getDateTimeInstance().format(new Date()));

          if (values.get(Scores.SCORE).toString() != "-1"){
            db = mOpenHelper.getWritableDatabase();
            rowId = db.insert(SCORES_TABLE_NAME, Scores.SCORE, values);
            if (rowId > 0) {
                Uri noteUri = ContentUris.withAppendedId(Questions.CONTENT_URI, rowId);
                getContext().getContentResolver().notifyChange(noteUri, null);
                return noteUri;
            }
      }
      break;
    }    

        throw new SQLException("Failed to insert row into " + uri);
  }


  @Override
  public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    int count;

    switch(sUriMatcher.match(uri)){
    case QUESTIONS:
            count = db.update(QUESTIONS_TABLE_NAME, values, where, whereArgs);
            break;

    case QUESTION_ID:
            String questionId = uri.getPathSegments().get(1);
            count = db.update(QUESTIONS_TABLE_NAME, values, Questions._ID + "=" + questionId
                    + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
      break;

    case SCORES:
            count = db.update(SCORES_TABLE_NAME, values, where, whereArgs);
      break;

    case SCORE_ID:
            String scoreId = uri.getPathSegments().get(1);
            count = db.update(SCORES_TABLE_NAME, values, Scores._ID + "=" + scoreId
                    + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
      break;

    default:
      throw new IllegalArgumentException("Unknown URI " + uri);
    }

        getContext().getContentResolver().notifyChange(uri, null);
    return count;
  }


  @Override
  public int delete(Uri uri, String where, String[] whereArgs) {
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    int count;

    switch(sUriMatcher.match(uri)){
    case QUESTIONS:
            count = db.delete(QUESTIONS_TABLE_NAME, where, whereArgs);
            break;

    case QUESTION_ID:
            String questionId = uri.getPathSegments().get(1);
            count = db.delete(QUESTIONS_TABLE_NAME, Questions._ID + "=" + questionId
                    + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
      break;

    case SCORES:
            count = db.delete(SCORES_TABLE_NAME, where, whereArgs);
      break;

    case SCORE_ID:
            String scoreId = uri.getPathSegments().get(1);
            count = db.delete(SCORES_TABLE_NAME, Scores._ID + "=" + scoreId
                    + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
      break;

    default:
      throw new IllegalArgumentException("Unknown URI " + uri);
    }

        getContext().getContentResolver().notifyChange(uri, null);
    return count;
  }


  @Override
  public String getType(Uri uri) {
        switch (sUriMatcher.match(uri)) {
        case QUESTIONS:
            return Questions.CONTENT_TYPE;
        case QUESTION_ID:
            return Questions.CONTENT_ITEM_TYPE;
        case SCORES:
            return Scores.CONTENT_TYPE;
        case SCORE_ID:
            return Scores.CONTENT_ITEM_TYPE;
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
  }


    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
          db.execSQL("CREATE TABLE questions ("
              + "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
              + "question TEXT,"
              + "answer INTEGER,"
              + "option1 TEXT,"
              + "option2 TEXT,"
              + "option3 TEXT,"
              + "option4 TEXT,"
              + "option5 TEXT,"
              + "option6 TEXT,"
              + "option7 TEXT,"
              + "option8 TEXT,"
              + "option9 TEXT,"
              + "option10 TEXT);");

            db.execSQL("CREATE TABLE scores ("
                + "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
                + "score NUMERIC,"
                + "date_created TEXT);");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
        }        
    }


    static {
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI(SATest.AUTHORITY, "questions", QUESTIONS);
        sUriMatcher.addURI(SATest.AUTHORITY, "questions/#", QUESTION_ID);
        sUriMatcher.addURI(SATest.AUTHORITY, "scores", SCORES);
        sUriMatcher.addURI(SATest.AUTHORITY, "scores/#", SCORE_ID);

        sQuestionsProjectionMap = new HashMap<String, String>();
        sQuestionsProjectionMap.put(Questions._ID, Questions._ID);
        sQuestionsProjectionMap.put(Questions.QUESTION, Questions.QUESTION);
        sQuestionsProjectionMap.put(Questions.ANSWER, Questions.ANSWER);
        sQuestionsProjectionMap.put(Questions.OPTION1, Questions.OPTION1);
        sQuestionsProjectionMap.put(Questions.OPTION2, Questions.OPTION2);
        sQuestionsProjectionMap.put(Questions.OPTION3, Questions.OPTION3);
        sQuestionsProjectionMap.put(Questions.OPTION4, Questions.OPTION4);
        sQuestionsProjectionMap.put(Questions.OPTION5, Questions.OPTION5);
        sQuestionsProjectionMap.put(Questions.OPTION6, Questions.OPTION6);
        sQuestionsProjectionMap.put(Questions.OPTION7, Questions.OPTION7);
        sQuestionsProjectionMap.put(Questions.OPTION8, Questions.OPTION8);
        sQuestionsProjectionMap.put(Questions.OPTION9, Questions.OPTION9);
        sQuestionsProjectionMap.put(Questions.OPTION10, Questions.OPTION10);

        sScoresProjectionMap = new HashMap<String, String>();
        sScoresProjectionMap.put(Scores._ID, Scores._ID);
        sScoresProjectionMap.put(Scores.SCORE, Scores.SCORE);
        sScoresProjectionMap.put(Scores.DATE_CREATED, Scores.DATE_CREATED);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:42:43+00:00Added an answer on May 14, 2026 at 9:42 pm

    If your consumer of the content provider is in the same application as the content provider itself, don’t use the content provider — go straight to the database. ContentProvider is designed for inter-process communication, so it adds overhead and limits your API.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.