Grumpy
Grumpy

Reputation: 165

Returning list of objects from Database

I am trying to return an object array from my database but can't get the call correct or the return object[s]. In the example below I just return an empty String trying to get this working. The listings[cnt] are being filled properly but I'm not sure how to make the call and return the Objects?

I am making 2 different calls to new Houses in getData() and that doesn't seem right either.

Houses[] listings = new Houses[cnt]; // Seems like I need this to initialize the array to a certain size. listings[cnt] = new Houses(z,a,b,c,d,e,f,g,h); // Then I fill the Object with variables.

then return listings somehow.

// ??? How do I set it up so that I get an array of objects back?
// I am making the call from MainActivity here
myHelper = new GrumpyDB(this);
    String h = new String();
    h = myHelper.getData();



public String getData() {
    Log.d(TAG, "Starting getData");
    SQLiteDatabase db = helper.getWritableDatabase();
    String[] columns = {myHelper.UID, myHelper.ADDRESS, myHelper.STREET, myHelper.CITY, myHelper.STATE, myHelper.ZIP, myHelper.ENTIRE_ADDRESS,myHelper.UPLOADS,myHelper.DOWNLOADS};
    Cursor cursor = db.query(myHelper.TABLE_NAME_LOCATION, columns, null, null, null, null, null);
    int cnt = cursor.getCount();
    Houses[] listings = new Houses[cnt];
    cnt = 0;
    while (cursor.moveToNext()) {
        int index0 = cursor.getColumnIndex(myHelper.UID);
        int index1 = cursor.getColumnIndex(myHelper.ADDRESS);
        int index2 = cursor.getColumnIndex(myHelper.STREET);
        int index3 = cursor.getColumnIndex(myHelper.CITY);
        int index4 = cursor.getColumnIndex(myHelper.STATE);
        int index5 = cursor.getColumnIndex(myHelper.ZIP);
        int index6 = cursor.getColumnIndex(myHelper.ENTIRE_ADDRESS);
        int index7 = cursor.getColumnIndex(myHelper.UPLOADS);
        int index8 = cursor.getColumnIndex(myHelper.DOWNLOADS);
        String z = cursor.getString(index0);
        String a = cursor.getString(index1);
        String b = cursor.getString(index2);
        String c = cursor.getString(index3);
        String d = cursor.getString(index4);
        String e = cursor.getString(index5);
        String f = cursor.getString(index6);
        String g = cursor.getString(index7);
        String h = cursor.getString(index8);
        listings[cnt] = new Houses(z,a,b,c,d,e,f,g,h);
        Log.d(TAG, listings[cnt].UID + listings[cnt].address + listings[cnt].street + listings[cnt].city + listings[cnt].state + listings[cnt].zip + listings[cnt].upload + listings[cnt].download);
        //buffer.append(f + "\n");
        cnt++;
    }
    //Log.d(TAG, "Returning From getData");
    //return listings;
    cursor.close();
    db.close();

    return "";
    //return listings;
}

Upvotes: 0

Views: 1069

Answers (2)

Grumpy
Grumpy

Reputation: 165

I got it ..... this works.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //GrumpyDB myHelper;
    myHelper = new GrumpyDB(this);
    //String h = new String();
    //h = myHelper.getData();
    //Houses House;
    Houses myList[] = myHelper.getData();
    for(int i =0; i < myList.length; i++) {
        Log.d(TAG,myList[i].street);
    }
    Button myBtn1 = (Button)findViewById(R.id.addHouseButton);
    myBtn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getApplicationContext(), AddHouse.class);
            intent.putExtra("func", "update"); // Use to pass items to next page
            startActivity(intent); // Start Activity
        }
    });
}

public Houses[] getData() {
    Log.d(TAG, "Starting getData");
    SQLiteDatabase db = helper.getWritableDatabase();
    String[] columns = {myHelper.UID, myHelper.ADDRESS, myHelper.STREET, myHelper.CITY, myHelper.STATE, myHelper.ZIP, myHelper.ENTIRE_ADDRESS, myHelper.UPLOADS, myHelper.DOWNLOADS};
    Cursor cursor = db.query(myHelper.TABLE_NAME_LOCATION, columns, null, null, null, null, null);
    int cnt = cursor.getCount();
    Houses[] listings;
    listings = new Houses[cnt];
    cnt = 0;
    while (cursor.moveToNext()) {
        int index0 = cursor.getColumnIndex(myHelper.UID);
        int index1 = cursor.getColumnIndex(myHelper.ADDRESS);
        int index2 = cursor.getColumnIndex(myHelper.STREET);
        int index3 = cursor.getColumnIndex(myHelper.CITY);
        int index4 = cursor.getColumnIndex(myHelper.STATE);
        int index5 = cursor.getColumnIndex(myHelper.ZIP);
        int index6 = cursor.getColumnIndex(myHelper.ENTIRE_ADDRESS);
        int index7 = cursor.getColumnIndex(myHelper.UPLOADS);
        int index8 = cursor.getColumnIndex(myHelper.DOWNLOADS);
        String z = cursor.getString(index0);
        String a = cursor.getString(index1);
        String b = cursor.getString(index2);
        String c = cursor.getString(index3);
        String d = cursor.getString(index4);
        String e = cursor.getString(index5);
        String f = cursor.getString(index6);
        String g = cursor.getString(index7);
        String h = cursor.getString(index8);
        listings[cnt] = new Houses(z, a, b, c, d, e, f, g, h);
        Log.d(TAG, listings[cnt].UID + listings[cnt].address + listings[cnt].street + listings[cnt].city + listings[cnt].state + listings[cnt].zip + listings[cnt].upload + listings[cnt].download);
        //buffer.append(f + "\n");
        cnt++;
    }
    //Log.d(TAG, "Returning From getData");
    //return listings;
    cursor.close();
    db.close();

    //return "";
    return listings;
}

Upvotes: 2

Karakuri
Karakuri

Reputation: 38605

Change your method signature to

public Houses[] getData() {

Upvotes: 1

Related Questions