Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
479 views
in Technique[技术] by (71.8m points)

android - 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, and the prev button to get the previous record and display it inside of my view. How would I call the next or previous record? I have looked for tutorials and stuff but didn't find any. I anyone has a tutorial please share with me. Thanks for any help. I wish I had some code to provide but I really don't know where to start.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

From my ContactView class

static long record = 1;

public void getData() {

    DBase db = new DBase(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 = new String[] { KEY_ROWID, KEY_ONE, KEY_TWO,
            KEY_THREE, KEY_FOUR, KEY_FIVE, KEY_SIX };
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "="
            + record, null, null, null, null);
    if (c != null && c.moveToFirst()) {
        String rRec = c.getString(0);
        String rOne = c.getString(1);
        String rTwo = c.getString(2);
        String rThree = c.getString(3);
        String rFour = c.getString(4);
        String rFive = c.getString(5);
        String rSix = c.getString(6);

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

        return rData;

    }
    return null;
}

and the next few are from my ContactView class

my buttons

@Override
public void onClick(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

public void recordPlus() {
    if (record < lastRecord) {
        record++;
    } else {
        record = firstRecord;
    }
    getData();

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

public void recordMinus() {
    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

public long fRec() {

    Cursor c = ourDatabase.query(DATABASE_TABLE, new String[] { "min(" + 
    KEY_ROWID
            + ")" }, null, null, null, null, null);
    c.moveToFirst();
    long rowID = c.getInt(0);

    return rowID;
}

}

public long lRec() {

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...