Skip to content Skip to sidebar Skip to footer

Getting The Next Record Into View From Database

I have two buttons inside of my application, one for next and one for prev. I want the next button to get the next record inside of my database and display it inside of my view, an

Solution 1:

I use an int to pull the record from the dbase.

From my ContactView class

staticlongrecord=1;

publicvoidgetData() {

    DBasedb=newDBase(this);
    db.open();
    lastRecord = db.lRec();
    firstRecord = db.fRec();
    rRec = db.getRec(record);
    db.close();

}

then my query is from my Dbase class

public String[] getRec(long record) {

    record = ContactView.record;

    String[] columns = newString[] { KEY_ROWID, KEY_ONE, KEY_TWO,
            KEY_THREE, KEY_FOUR, KEY_FIVE, KEY_SIX };
    Cursorc= ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "="
            + record, null, null, null, null);
    if (c != null && c.moveToFirst()) {
        StringrRec= c.getString(0);
        StringrOne= c.getString(1);
        StringrTwo= c.getString(2);
        StringrThree= c.getString(3);
        StringrFour= c.getString(4);
        StringrFive= c.getString(5);
        StringrSix= c.getString(6);

        String[] rData = { rRec, rOne, rTwo, rThree, rFour,
                rFive, rSix };

        return rData;

    }
    returnnull;
}

and the next few are from my ContactView class

my buttons

@OverridepublicvoidonClick(View arg0) {

    switch (arg0.getId()) {
    case R.id.bSQLvPrev:

        recordMinus();
        display();

        break;
    case R.id.bSQLvNext:

        recordPlus();
        display();

        break;

    }
}

and the methods they call

public void display() {

    etSQLvRec.setText(rRec[0]);
    etSQLvOne.setText(rRec[1]);
    etSQLvTwo.setText(rRec[2]);
    etSQLvThree.setText(rRec[3]);
    etSQLvFour.setText(rRec[4]);
    etSQLvFive.setText(rRec[5]);
    etSQLvSix.setText(rRec[6]);
}

public void recordPlus() {
        record++;

    }

public void recordMinus() {
        record--;
    }

That will get the record from the database based on the "record" variable, and the buttons increment it, or decrement it, it also skips any "empty" records.

EDIT OK, I had changed some stuff around since I lasted used my db, so use the next recordPlus() and recordMinus() code instead

publicvoidrecordPlus() {
    if (record < lastRecord) {
        record++;
    } else {
        record = firstRecord;
    }
    getData();

    do {
        if (record < lastRecord) {
            record++;
        } else {
            record = firstRecord;
        }
        getData();
    } while (rRec == null);
}

publicvoidrecordMinus() {
    if (record == 1) {
        record = lastRecord;
    } else {
        record--;
    }
    getData();

    do {
        if (record == 1) {
            record = lastRecord;
        } else {
            record--;
        }
        getData();
    } while (rRec == null);
}

And you'll need my fRec() and lRec() which find the first and last records in the DB

publiclongfRec() {

    Cursorc= ourDatabase.query(DATABASE_TABLE, newString[] { "min(" + 
    KEY_ROWID
            + ")" }, null, null, null, null, null);
    c.moveToFirst();
    longrowID= c.getInt(0);

    return rowID;
}

}

publiclonglRec() {

    longlastRec=0;
    Stringquery="SELECT ROWID from Table order by ROWID DESC limit 1";
    Cursorc= ourDatabase.rawQuery(query, null);
    if (c != null && c.moveToFirst()) {
        lastRec = c.getLong(0);
    }
    return lastRec;
}

Post a Comment for "Getting The Next Record Into View From Database"