获取下一条记录到视图从数据库 [英] Getting the next record into view from database

查看:222
本文介绍了获取下一条记录到视图从数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的应用程序内部的两个按钮,一个是下一个,一个是preV。我想下一个按钮来获得下一个记录我的数据库里面和我的观点里显示出来,和$ P $光伏按钮获得previous记录和我的视图中显示它。我怎么会叫下一个或previous记录?我看过的教程之类的东西,但没有发现任何。我任何人有一个教程,请与我分享。感谢您的帮助。我希望我有一些code提供,但我真的不知道从哪里开始。

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.

推荐答案

我用一个int从质数据库拉备案。

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

这是我ContactView类

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();

}

然后我的查询是从我的dBase类

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;
}

和未来几年来自我ContactView类

and the next few are from my ContactView class

我的按钮

@Override
public void onClick(View arg0) {

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

        recordMinus();
        display();

        break;
    case R.id.bSQLvNext:

        recordPlus();
        display();

        break;

    }
}

和方法,他们称之为

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.

编辑好了,我已经改变了一些东西左右,因为我持续用我的分贝,因此使用下一个recordPlus()和recordMinus()code,而不是

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);
}

和你需要我的FREC()和LREC(),它发现在DB的第一个和最后一个记录

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;
}

这篇关于获取下一条记录到视图从数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆