0

I have a SQLite Database with 45 different entries, each with:

public static final String TABLE = "Table";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_HOUR = "hour";
public static final String COLUMN_WEEK = "week";
public static final String COLUMN_DAY = "day";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_DESCRIPTION = "description";
public static final String COLUMN_COLOUR = "colour";
public static final String COLUMN_ROOM = "room";

now I want to read out all. I Do this with following:

public Cursor fetchAllSubject(){
    Cursor mCursor = database.query(true, TABLE, new String[] {
            COLUMN_ID, COLUMN_HOUR, COLUMN_WEEK, COLUMN_DAY, COLUMN_NAME, COLUMN_DESCRIPTION, COLUMN_COLOUR, COLUMN_ROOM},null
            , null, null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

In a other class I have this code to read all out:

dao = new DAO(this);
Cursor subjectList = dao.fetchAllSubject();

Now I want to have for each entry an array with ID, Hour, week, ... but I have no idea how to do that. My first try was following:

ArrayList<String> mo1h = new ArrayList<String>();
subjectList.moveToFirst();
while(!subjectList.isAfterLast()) {
     mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ID)));
     mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_HOUR)));
     mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_WEEK)));
     mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_DAY)));
     mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_NAME)));
     mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_DESCRIPTION)));
     mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_COLOUR)));
     mo1h.add(subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ROOM)));     
     subjectList.moveToNext();
}

But everything is in mo1h, and I dont know how to devide it. The best would be to have a String[] for each. Has anybody an Idea? Thanks!

2 Answers 2

4

You can create on Bean class and then create one ArrayList (Collection class)

public class Bean
{
   public Bean();
   String id, hour, week, day, name, description, color, room;
}

now create list of Bean

ArrayList<Bean> mo1h = new ArrayList<Bean>();
subjectList.moveToFirst();
while(!subjectList.isAfterLast()) {
Bean b = new Bean();
   b.id = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ID));
   b.hour =subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_HOUR));
   ...
   ...
   // all your column
   mo1h.add(b);
}
Sign up to request clarification or add additional context in comments.

Comments

2

Why not continue with your strategy, but instead use an ArrayList of String[]:

ArrayList<String[]> mo1h = new ArrayList<String[]>();
subjectList.moveToFirst();
while(!subjectList.isAfterLast()) {
     String[] toUse = new String[8];
     toUse[0] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ID));
     toUse[1] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_HOUR));
     toUse[2] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_WEEK));
     toUse[3] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_DAY));
     toUse[4] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_NAME));
     toUse[5] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_DESCRIPTION));
     toUse[6] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_COLOUR));
     toUse[7] = subjectList.getString(subjectList.getColumnIndex(dao.COLUMN_ROOM)); 
     mo1h.add(toUse);    
     subjectList.moveToNext();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.