How to use a get method for a SQLite Database mana

2019-08-08 17:02发布

问题:

What I am trying to do is retrieve an ArrayList from another database manager class. Unfortunately all I can do because the manager class cannot work statically is create an instance in another class, then call the method. Then I got myself into passing that same instance into the method which asked for an SQLiteDatabase object. Now I've worked myself into a bind of confusion, when all I really want is to do is retrieve the arraylist to display a listview of elements from an SQL column.

EDIT: My post lacked clarity, so I'll try to specify exactly what is going wrong and what I am trying to accomplish here:

In a display (output) activity, I am trying to use a ListView to display elements contained in an SQL database. Currently, I am only focusing on one column (Assignment Names). My approach involved using a get method built into the database manager class, but because you cannot reference that method statically, I tried to use the method by creating an instance of that manager class. This would return an ArrayList of Inputted objects (each containing a name). It seemed to have worked, but when running the program, the LogCat protested that I was calling getDatabase recursively. After looking online, people recommended that I fix the issue by changing the method to ask for (SQLiteDatabase db) as parameters so the same database gets tossed around in the manager. Now I get confused here-- I'm not sure what to pass into this method from the display activity. It also doesn't help that from what I've heard from the comments, my get method doesn't traverse the SQL database properly. If you can solve this puzzle THANK YOU!

I'll post my code for diagnosis, hopefully an outside view will show exactly what's wrong with everything I'm trying here.

public Cursor getAssignmentNames(SQLiteDatabase db) {
    return db.query(false, ASSIGNMENT_TABLE, COLUMN_TITLES, 
            " WHERE " + ASSIGNMENT_NAME + " ", null, null, null, " ORDER BY "+ASSIGNMENT_URGENCY_RATING, null);
}

/

    public ArrayList<Inputted> getListOfAssignments (SQLiteDatabase db) {
        Cursor names = getAssignmentNames(db);
        ArrayList<Inputted> assList = new ArrayList<Inputted>();
        names.moveToFirst();
        while (!cursorsAreAfterLast(names) ) {
            int go = 0;
            assList.add(new Inputted(names.getString(go))

            names.moveToNext();
            go++;
        }
        return assList;
    }

/

DBRecordsLayer assignmentRecords = new DBRecordsLayer(this,
                "assignment.db", null, 1);


        ArrayList<Inputted> assList = DBRecordsLayer.getListOfAssignments(assignmentRecords);

回答1:

Your code is a bit confusing... In each iteration of the while loop, you are incrementing the cursor (names.moveToNext()); You are also incrementing go.

The result would be:

1st iteration: You are taking the data from the first column of the first query

2nd iteration: You are taking the data from the second column of the second query

etc...

I'm assume that you want to be reading data from the same column of the database for each iteration.

try this:

public ArrayList<Inputted> getListOfAssignments (SQLiteDatabase db) {
    Cursor names = getAssignmentNames(db);
    ArrayList<Inputted> assList = new ArrayList<Inputted>();

    names.moveToFirst();
    columnContainingStringToSendToInputtedConstructor = x;  //replace the x with column you need from your table
    while (!names.isAfterLast()) {
        assList.add(new Inputted(names.getString(columnContainingStringToSendToInputtedConstructor));

        names.moveToNext();
    }
}