【问题标题】:how to increment numeric string value in Android如何在Android中增加数字字符串值
【发布时间】:2016-08-27 19:25:28
【问题描述】:

我有一个 android 应用程序,它使用光标从表中返回一个字符串值(所以我认为只有字符串),当我尝试解析 int 时,我得到了捕获异常,所以我不能增加它,我需要的只是每次调用时,它都会从 0 变为 1,2,3,5,100,923021312 等。

如果有人知道我错了,如何将其转换为 int 或如何将其直接作为 int 从数据库中取出

public String dmst(String id_math)
{
    String kleidi="";
    int efedriko=0;
    String parousies_max="0";
    final String SQL_code = "SELECT "+DatabaseHelper.Col_PAROUSIES_MAX+" FROM "+DatabaseHelper.TABLE_NAME2+" WHERE "+DatabaseHelper.Col_ID_MATHIMA+" = "+id_math;
    SQLiteDatabase sqLiteDatabase=this.getReadableDatabase();
    Cursor cursor = sqLiteDatabase.rawQuery(SQL_code,null);
    if (cursor.moveToFirst()) {
        do {
            parousies_max = cursor.getString(cursor.getColumnIndex("PAROUSIES_MAX"));
        }while (cursor.moveToNext());
    }
    cursor.close();

    try {
        efedriko = Integer.parseInt(parousies_max);
    } catch(NumberFormatException nfe) {}
    efedriko=efedriko+1;

【问题讨论】:

  • 首先不要捕获异常,就这样扔掉它。打印异常,以便您可以查看错误消息的实际内容。其次,请不要在没有发布实际错误消息的情况下在这里询问有关代码中的错误或异常的问题。
  • 任何编程语言中的字符串都不是数学事物,无论它是否包含数字符号。您可以将字符串转换为 float、int、long、double,然后对其进行数学运算。

标签: java android android-studio cursor


【解决方案1】:

将 do 循环中的行从 getString 更改为 getInt 方法:

来自:

parousies_max = cursor.getString(cursor.getColumnIndex("PAROUSIES_MAX"));

到:

parousies_max = cursor.getInt(cursor.getColumnIndex("PAROUSIES_MAX"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-09
    • 2014-06-09
    • 2021-07-17
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2012-11-06
    相关资源
    最近更新 更多