thunderousNinja
thunderousNinja

Reputation: 3520

How to get specific rows from ContentProvider?

I am a little new to using content providers. I was wondering how I would get specific rows from a content provider?

For example how would I get the first row of my provider?

This is what I tried but It isnt working:

final Cursor cursorConversations = getActivity().getContentResolver()
                .query(CONTENT_URI, null, null, null, null);

Toast.makeText(
        getActivity(),
        cursorConversations.getString(cursorConversations
        .getColumnIndex(Columns.TITLE)),
        Toast.LENGTH_LONG).show();

Upvotes: 1

Views: 1344

Answers (2)

Deepanker Chaudhary
Deepanker Chaudhary

Reputation: 1704

use something like this:---

if(cursorConversations.moveToFirst()){
 int size=cursorConversations.getCount();
 for(int i=0;i<size;i++){
 cursorConversations.getString(cursorConversations
    .getColumnIndex(Columns.TITLE));
 cursorConversations.moveToNext();
}
}
cursorConversations.close();

Or

       while(cursorConversations.moveTonext())
      {
          cursorConversations.getString(cursorConversations
    .getColumnIndex(Columns.TITLE));
       }

Upvotes: 2

mango
mango

Reputation: 5636

you simply use cursor move methods ex:

    cursorConversations.moveToFirst();
    cursorConversations.moveToPosition(0);
    cursorConversations.moveToNext(); // <-- if at beginning position

just to make this answer a little more meaty, a popular technique used to loop through the rows of the cursor 1 by 1 from the beginning is:

    while (cursorConversations.moveToNext()) {
        // do something
    }

Because the moveToNext() method (as well as other move methods) return a boolean, the loop will exit when the last row has been reached and can no longer moved to the next. effective and easy on the eyes too. One more tip: the cursor starts at the -1 index, before the first position of a zero-based query index results.

Upvotes: 2

Related Questions