0

So, I want to build an app to view some data from database. I already have the database, also already made some entities that have exactly same properties name with the column names in database. And also I put the database into database directory by copying from assets folder.

What I want to achieve is, I want to pull some data, and put it into array list, so I can show it in ListView in fragment.

Is there any convenient way to pull some data without querying (like loadAll() function) ?

For now, I'm using cursor to save the pulled data using query, and assign its properties one by one using set function like setName(String name).

After that, I show the list using CursorAdapter.

It would be like this

public class FrameCursor extends CursorWrapper{

    /**
     * Creates a cursor wrapper.
     *
     * @param cursor The underlying cursor to wrap.
     */
    public FrameCursor(Cursor cursor) {
        super(cursor);
    }

    public ZFrame getFrame(){
        if(isBeforeFirst() || isAfterLast()){
            return null;
        }

        ZFrame frame = new ZFrame();


        ZFrameDao frameDao = new ZFrameDao();


        int frameEdition = getInt(getColumnIndex(COLUMN_FRAME_EDITION));
        int frameId = getInt(getColumnIndex(COLUMN_FRAME_ID));
        int frameNumber = getInt(getColumnIndex(COLUMN_FRAME_NUMBER));
        int frameType = getInt(getColumnIndex(COLUMN_FRAME_TYPE));
        int frameBookmark = getInt(getColumnIndex(COLUMN_FRAME_BOOKMARK));
        int frameGlyph = getInt(getColumnIndex(COLUMN_FRAME_GLYPH));
        int frameLesson = getInt(getColumnIndex(COLUMN_FRAME_LESSON));
        String frameAllReading = getString(getColumnIndex(COLUMN_FRAME_ALL_READING));
        String frameReadingNumber = getString(getColumnIndex(COLUMN_FRAME_READING_NUMBER));
        String frameReference = getString(getColumnIndex(COLUMN_FRAME_REFERENCE));
        String frameWritingNumber = getString(getColumnIndex(COLUMN_FRAME_WRITING_NUMBER));


        frame.setZEDITION(frameEdition);
        frame.setZFRAME_ID(frameId);
        frame.setZFRAME_NUMBER(frameNumber);
        frame.setZFRAME_TYPE(frameType);
        frame.setZBOOKMARK(frameBookmark);
        frame.setZGLYPH((long)frameGlyph);
        frame.setZLESSON((long)frameLesson);
        frame.setZALL_READING_NUMBER(frameAllReading);
        frame.setZREADING_NUMBER(frameReadingNumber);
        frame.setZREFERENCE(frameReference);
        frame.setZWRITING_NUMBER(frameWritingNumber);



        return frame;
    }
}

It would be consume lot of work for doing this for every table.

So anyone could help me?

Bhavesh Hirpara
  • 22,255
  • 15
  • 63
  • 104
Gabriel Juan
  • 77
  • 1
  • 8
  • I mean, by not using the rawquery like select "columns" from table where .... But using like query builder, or some other function that do the query for you – Gabriel Juan Feb 16 '15 at 05:59

2 Answers2

0

Why not using CursorLoader ? Use CursorLoader to handle the cursor query issue, and it works perfectly with CursorAdapter, Here is the google's guide

jobcrazy
  • 1,073
  • 10
  • 16
  • Yeah, I could use that perfectly fine with raw query, or SQLite Helper default implementation of getReadableDatabase().query function. But, the greenDAO function mostly will return List type instead of cursor. Like queryDeep that will return List instead of cursor. So, I think that this would be done using other adapter like array adapter, or list adapter. – Gabriel Juan Feb 16 '15 at 07:18
  • Bind cursor values to your data object is a necessary phase. Maybe you can refactor the "cursor parse" code to become a method of the data object's class while which just change the position of the code. – jobcrazy Feb 16 '15 at 07:41
  • Hmm, I understand your point, so you are saying that I should stick with the cursor one rather than using the List returned from the function ? could you please give me some example code by editing your answer ? – Gabriel Juan Feb 16 '15 at 07:52
  • https://developer.android.com/guide/components/loaders.html. As I listed above, google's guide is very clear and has an example at the end of the guide, the key point is using LoaderManager, and you need to wrap your database extending ContentProvider. There is also an example here : http://developer.android.com/reference/android/app/LoaderManager.html – jobcrazy Feb 16 '15 at 08:03
0

Maybe this is what you need, a light weight orm api? You can go here for more information.

Bilal
  • 109
  • 1
  • 5
  • Actually, when I surfing the internet back there, I found that the greenDAO is more powerful and easier to use then the ORM Lite. Anyway thanks for answering – Gabriel Juan Feb 16 '15 at 07:19