首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >android :获取错误的值

android :获取错误的值
EN

Stack Overflow用户
提问于 2012-09-29 23:06:42
回答 1查看 2.3K关注 0票数 1

目前,我遇到了很多问题,在android与sqlite数据库。这次真的很奇怪。

每次我进入一个特定的活动,我想显示最新的三个条目,如“团队1比2的目标-目标”。奇怪的是,数据库正在返回错误的值。让我们从当前的数据库内容开始:

对于我的问题来说,重要的是"id“、"goals1”和"goals2“列。正如你所看到的,最近的三个游戏是"4: dfsadf vs. asf 3-2","3: df vs.as 3-2“和"2: dfsadf vs.dfas 2-0”。在我的活动中,以下内容如下:

正如您所看到的,客场团队目标的目标是错误的(看起来条目的ID被设置为客场目标)。我设置这三个按钮的文本如下:

代码语言:javascript
运行
复制
entries = datasource.getLatestFootballEntry(challengeName);
button = (Button) findViewById(R.id.bt_overview_latest1);
            entry = entries.elementAt(0);
            buttonText = entry.date + ": " + entry.team1 + " vs. " + entry.team2 + " " + entry.goals1 + "-" + entry.goals2;
            button.setText(buttonText);
            button.setClickable(true);

            button = (Button) findViewById(R.id.bt_overview_latest2);
            entry = entries.elementAt(1);
            buttonText = entry.date + ": " + entry.team1 + " vs. " + entry.team2 + " " + entry.goals1 + "-" + entry.goals2;
            button.setText(buttonText);
            button.setClickable(true);

            button = (Button) findViewById(R.id.bt_overview_latest3);
            entry = entries.elementAt(2);
            buttonText = entry.date + ": " + entry.team1 + " vs. " + entry.team2 + " " + entry.goals1 + "-" + entry.goals2;
            button.setText(buttonText);
            button.setClickable(true);

在这里,我得到了具体的条目:

代码语言:javascript
运行
复制
public Vector<FootballEntry> getLatestFootballEntry(String challengeName){
        Vector<FootballEntry> entries = new Vector<FootballEntry>();
        Cursor cursor = database.query(CustomSQLiteHelper.TABLE_FOOTBALL, allColumns2, null, null, null, null, null);
        cursor.moveToLast();
        int i=0;
        while(i<3 && !cursor.isBeforeFirst()){
            if(checkIfCorrectChallengeName(cursor, challengeName)){
                entries.add(cursorToFootballEntry(cursor));
                i++;
            }
            cursor.moveToPrevious();
        }
        return entries;
    }

现在奇怪的部分来了。当我按照在按钮上设置文本的方法进行更改时,一切都很好。

代码语言:javascript
运行
复制
button = (Button) findViewById(R.id.bt_overview_latest1);
            datasource.open();
            entry = datasource.getSpecificFootballEntry(entries.elementAt(0).id);
            datasource.close();
            buttonText = entry.date + ": " + entry.team1 + " vs. " + entry.team2 + " " + entry.goals1 + "-" + entry.goals2;
            button.setText(buttonText);
            button.setClickable(true);

现在拆开

这是我的cursorToFootballEntry方法

代码语言:javascript
运行
复制
private FootballEntry cursorToFootballEntry(Cursor cursor){
    FootballEntry entry = new FootballEntry();
    entry.id = cursor.getLong(0);
    entry.challengeName = cursor.getString(1);
    entry.date = cursor.getString(2);
    entry.home = cursor.getInt(3);
    entry.platform = cursor.getString(4);
    entry.team1 = cursor.getString(5);
    entry.stars1 = cursor.getDouble(6);
    entry.team2 = cursor.getString(7);
    entry.stars2 = cursor.getDouble(8);
    entry.goals1 = cursor.getInt(9);
    Log.i("dfasdf", "IN CURSOR TO FOOENRTY  " + cursor.getInt(9) + "-" + cursor.getInt(10) + "   id: " + cursor.getLong(0));
    entry.goals2 = cursor.getInt(10);
    entry.finished = cursor.getInt(11);
    return entry;
}

正如您所看到的,我将一些信息打印到日志中(即团队目标的两个值)。输出是

代码语言:javascript
运行
复制
09-29 22:50:18.191: I/dfasdf(7039): IN CURSOR TO FOOENRTY  3-4   id: 4
09-29 22:50:18.191: I/dfasdf(7039): IN CURSOR TO FOOENRTY  3-3   id: 3
09-29 22:50:18.201: I/dfasdf(7039): IN CURSOR TO FOOENRTY  2-2   id: 2
09-29 22:50:18.241: I/dfasdf(7039): IN CURSOR TO FOOENRTY  3-2   id: 4

因此,基本上,ID 4的条目被读出两次,但是有两个不同的离开目标值。是什么导致了这一切?我的意思是,我是一次三次还是一次三次,只要他们是相同的游戏,显然是什么(看到id的)。得到一个单一的游戏总是返回正确的值。同样的事情会发生,如果我得到所有的游戏(在不同的活动中需要)。在数据库创建声明中,只有一个值设置为“自动增量”,而_id则没有其他值。

现在,为了“证明”这一点,我输入了一个新条目,分数1-0:下面是displaid (设置文本的第二个变体,意味着第一个将是正确的)

日志

代码语言:javascript
运行
复制
09-29 23:02:13.281: I/dfasdf(7039): IN CURSOR TO FOOENRTY  1-5   id: 5
09-29 23:02:13.291: I/dfasdf(7039): IN CURSOR TO FOOENRTY  3-4   id: 4
09-29 23:02:13.291: I/dfasdf(7039): IN CURSOR TO FOOENRTY  3-3   id: 3
09-29 23:02:13.311: I/dfasdf(7039): IN CURSOR TO FOOENRTY  1-0   id: 5

编辑:当然,解决方案是只获取最新游戏的ID,然后单独获取它们,但我问题的动机更多地是为了理解为什么会发生这种情况。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-30 09:47:05

通过光标向后移动似乎不起作用。

但是,如果没有显式指定排序,则无法保证按任何特定顺序返回记录,因此修改查询将更加容易,以便它准确地返回所需的记录:

代码语言:javascript
运行
复制
cursor = database.query(CustomSQLiteHelper.TABLE_FOOTBALL,
                        allColumns2,
                        null, null,
                        null, null,
                        "id DESC",  // return largest id values first
                        "3");       // return no more than 3 records
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
    ...
    cursor.moveToNext();
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12657286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档