How to use a get method for a SQLite Database management class in another class?
Asked Answered
M

1

1

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);
Menadione answered 30/12, 2013 at 1:0 Comment(11)
public ArrayList<Inputted> getListOfAssignments declares that this method will return an instance of ArrayList<Inputted>.Collage
I get that, but how do I get it to return that object while passing in a db object (which I assume is necessary because my issue before wsd calling the database recursively)Menadione
Use return statement. In your case return assList. Technically you will be returning a reference to ArrayList object, but in Java it's meant the same.Collage
Sorry, that was just a paste issue. I have it there now.Menadione
what is the point of this go++;Salmons
Now that we cleared this little issue, what is your exact question here?Collage
It should specify the index of the names in which to place the name item from the SQL table... so there's one problem, it should be set/addMenadione
I still fail to understand what you're trying to achieve here. Please consider rewording your post and/or add more relevant details.Collage
Alright, I edited the post to make it more understandable as to what I'm getting at. Hopefully you can help!Menadione
@Menadione : I'm assuming DBRecordsLayer extends SQLiteOpenHelper? Correct?Iliac
Yes, I should have included thatMenadione
M
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();
    }
}
Merow answered 30/12, 2013 at 1:35 Comment(3)
But how to get the method to return an <Inputted>ArrayList to another class??Menadione
return assList; will return the ArrayList the function creates to the callerMerow
sorry that was a pasting issue. It includes the return method that was there before now.Menadione

© 2022 - 2024 — McMap. All rights reserved.