Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 安卓时间排名_Java_Android_Database_Sqlite - Fatal编程技术网

Java 安卓时间排名

Java 安卓时间排名,java,android,database,sqlite,Java,Android,Database,Sqlite,我目前在android应用程序上的时间排名有问题。我正在使用sqlite数据库,该数据库将存储用户完成解决问题的时间。稍后我将使用sqlite数据库来显示玩游戏的用户的排名 例如,玩家在大约1:25(1分25秒)的时间内完成了拼图,应用程序将在sqlite数据库中存储1:25。但我在做这件事上遇到了问题 我可以将其作为字符串存储在sqlite上,但不能使用顺序 通过 我尝试将其存储为int,但输出显示:无效int 在删除冒号(:)时,我尝试将其存储为字符串,但上的数据库返回“125” 我的问

我目前在android应用程序上的时间排名有问题。我正在使用
sqlite
数据库,该数据库将存储用户完成解决问题的时间。稍后我将使用
sqlite
数据库来显示玩游戏的用户的排名

例如,玩家在大约1:25(1分25秒)的时间内完成了拼图,应用程序将在sqlite数据库中存储
1:25
。但我在做这件事上遇到了问题

  • 我可以将其作为
    字符串存储在
    sqlite
    上,但不能使用
    顺序
    通过

  • 我尝试将其存储为
    int
    ,但输出显示:无效
    int

  • 在删除冒号()时,我尝试将其存储为
    字符串,但上的数据库返回“125”

我的问题是:
sqlite
数据库上存储特定时间值的最佳方式是什么?

我阅读了有关时间和日期函数的文档,但它似乎不能应用于自定义时间。它只适用于当前日期或当前时间。拜托,我需要帮助。我是android和sqlite的业余爱好者。这个项目是我为这个sem撰写的论文

接受任何意见和建议。提前谢谢

顺便说一下,这是检索sqlite上所有数据的代码。这是一个测试程序,测试我的代码/修改代码,看看它们是否正常工作

try {
        String selectQuery = "SELECT * FROM " + DatabaseHelper.TABLE_OUTLET + " WHERE category LIKE '" + cat + "' ORDER BY category ASC";
        Cursor cursor = db.rawQuery(selectQuery, null);
        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {

                // Read columns data
                int outlet_id = cursor.getInt(cursor.getColumnIndex("outlet_id"));
                String outlet_name = cursor.getString(cursor.getColumnIndex("outlet_name"));
                String outlet_type = cursor.getString(cursor.getColumnIndex("outlet_type"));
                String category = cursor.getString(cursor.getColumnIndex("category"));


                // dara rows
                TableRow row = new TableRow(context);
                row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
                        TableLayout.LayoutParams.WRAP_CONTENT));
                String[] colText = {outlet_name, outlet_type, category};
                for (String text : colText) {
                    TextView tv = new TextView(this);
                    tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
                            TableRow.LayoutParams.WRAP_CONTENT));
                    tv.setGravity(Gravity.CENTER);
                    tv.setTextSize(16);
                    tv.setPadding(5, 5, 5, 5);
                    tv.setText(text);
                    row.addView(tv);
                }
                tableLayout.addView(row);

            }

        }
        db.setTransactionSuccessful();

    } catch (SQLiteException e) {
        e.printStackTrace();

    } finally {
        db.endTransaction();
        // End the transaction.
        db.close();
        // Close database
    }
}

我建议您将时间转换为秒,并将其存储为
integer

使用integer作为数据库类型。检查可用类型。然后,需要使用标准java类或任何其他类型(如果愿意)将整数值转换为时间格式


使用INTEGER还可以轻松地对它们进行排序。

1:25
将其转换为秒

1*60+25=85
。所以存储
85

每次你想储存它时,都要这样做。您可以在恢复时执行相反的操作

例如

sec = 85 % 60 = 25
min = 85 / 60 = 1

根据@shryashssarnayak的建议,您将获得
1:25

,我已设法解决了此问题

这是生成的代码:

try {
            String selectQuery = "SELECT * FROM " + DatabaseHelper.TABLE_OUTLET + " WHERE category LIKE '" + cat + "' ORDER BY outlet_type ASC";
            Cursor cursor = db.rawQuery(selectQuery, null);
            if (cursor.getCount() > 0) {
                while (cursor.moveToNext()) {

                    // Read columns data
                    int outlet_id = cursor.getInt(cursor.getColumnIndex("outlet_id"));
                    String outlet_name = cursor.getString(cursor.getColumnIndex("outlet_name"));
                    int outlet_type = Integer.parseInt(cursor.getString(cursor.getColumnIndex("outlet_type")));
                    String category = cursor.getString(cursor.getColumnIndex("category"));


                    // dara rows
                    TableRow row = new TableRow(context);
                    row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
                            TableLayout.LayoutParams.WRAP_CONTENT));
                    int m, s;
                    s = outlet_type % 60;
                    m = outlet_type / 60;

                    String[] colText = {outlet_name, String.format("%02d:%02d", m, s), category};
                    for (String text : colText) {
                        TextView tv = new TextView(this);
                        tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
                                TableRow.LayoutParams.WRAP_CONTENT));
                        tv.setGravity(Gravity.CENTER);
                        tv.setTextSize(16);
                        tv.setPadding(5, 5, 5, 5);
                        tv.setText(text);
                        row.addView(tv);
                    }
                    tableLayout.addView(row);

                }

            }
            db.setTransactionSuccessful();

        } catch (SQLiteException e) {
            e.printStackTrace();

        } finally {
            db.endTransaction();
            // End the transaction.
            db.close();
            // Close database
        }
    }

您可以将整个内容转换为秒并存储。@shryashssarnayak我需要先转换并存储在数据库中,还是先存储然后在检索数据时再转换?“我尝试将其存储为int,但输出显示:invalid int”,这一定是有缺陷的,你能展开吗?@petey例如,我在数据库中存储了01:25。我使用Integer.parseInt(“01:25”)。错误显示:无效
int
:01:25。我想这是因为冒号(:)我应该先转换它吗?或者在检索数据时进行转换?在将时间保存到数据库之前进行转换。稍后您将能够从数据库中以秒为单位检索时间谢谢您的建议。我认为这个答案比其他答案更清楚。谢谢你。我要试试这个。