Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.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
Android-检查SQLite DB表中的值_Android_Sqlite_Textview - Fatal编程技术网

Android-检查SQLite DB表中的值

Android-检查SQLite DB表中的值,android,sqlite,textview,Android,Sqlite,Textview,我正在尝试从新删除的SQLite db表行读取数据 基本上,当某个活动加载时,我的程序将删除一行,我想检查行中的值 这是我的get代码: public String getSlot() { String slots = new String(); Cursor cursor = database.query(ChosenSlotDatabaseHandler.TABLE_CHOSEN_PARKING_SLOT, c

我正在尝试从新删除的SQLite db表行读取数据

基本上,当某个活动加载时,我的程序将删除一行,我想检查行中的值

这是我的
get
代码:

public String getSlot() {
            String slots = new String();

            Cursor cursor = database.query(ChosenSlotDatabaseHandler.TABLE_CHOSEN_PARKING_SLOT,
                chosenSlotColumn, null, null, null, null, null);

            if( cursor.moveToFirst() ) {
                slots = cursor.getString(0);
            }
            // make sure to close the cursor
            cursor.close();
            return slots;
          }
我已使用以下代码删除该值:

public static final String DELETE_SLOT = "DELETE FROM "
            + TABLE_CHOSEN_PARKING_SLOT + " WHERE "
            + "_ID = " + CHOSEN_ID + ";";

public void deleteSlotSQL()
      {
          database.execSQL(ChosenSlotDatabaseHandler.DELETE_SLOT);
      }
这是我设置
TextView
值的条件:

if(slots == null)
            {
                chosenSlotView.setText("You have not parked");
            }
            else
            {
                chosenSlotView.setText("You are parked in : " + slots);
            }
起初,我认为一旦删除了唯一一行,
getSlot()
将返回null,但从我运行的
Log.d
中,它似乎不是null:

if(slots != null)
            {
               Log.d("notnull","not null dude");
            }else
            {
               Log.d("null","Yay null");
            }
log
返回“notnull dude”


任何关于如何获取
插槽
值的建议,以便我可以设置
文本视图
值???

首先,您应该避免使用诸如“notnull dude”之类的日志,您会发现这些日志既有趣又有启发性,但过了一段时间,我们将无法编写干净专业的代码。 我的第二个建议是使用常量而不是hadcoded字符串。创建一个类常量并在其中添加字符串

   public static final String NOT_PARKED = "You have not parked"
我的第三个建议是看看ormlite


如果日志正常,则textview可能有问题。尝试将文本放在textview中的条件之前,以检查是否将设置该条件。还要检查布局文件

您的
插槽
绝对不能为空,因为: 字符串槽=新字符串()

应该是

 String slots = null;
Cursor cursor = database.query(ChosenSlotDatabaseHandler.TABLE_CHOSEN_PARKING_SLOT,
                chosenSlotColumn, null, null, null, null, null);

            if( cursor.moveToFirst() ) {
                slots = cursor.getString(0);
            }
            // make sure to close the cursor
            cursor.close();
            return slots;
编辑:

不必指定
字符串slot=null

尝试使用:

if(slots.isEmpty())
        {
            chosenSlotView.setText(notParked);
        }
        else
        {
            chosenSlotView.setText(isParked + slots);
        }

首先,你应该避免使用像“notnull dude”这样的日志,你会发现它们很有趣,很有启发性,但是过了一段时间,我们就无法编写干净专业的代码了。不,那个日志只是调试的目的,我只是查看一下。稍后会将其删除或更改为更专业的句子:)但在
if
子句中,有
slots=cursor.getString(0)如何获取
getString(0)
的值?能否提供ChosenSlotDatabaseHandler.TABLE_Selected_PARKING_SLOT字符串定义?是否为
Selected_PARKING_SLOT
。。这就是罪魁祸首?直接使用
chosenSlotView.setText(插槽)尝试
,则代码>在
之外,但它不显示任何输出chosenSlotView.setText(“测试文本:+slots”);因为插槽可以是空字符串,如“”;啊,是的,我把它们变成了常数。。我认为这对我来说很方便,但是是的,考虑到模块化,这是一个错误的决定。。
TextView
正常,它仍然显示从这里获得的值
chosenSlotView.setText(“您停在:“+slots”)