0

I'm trying to read data from newly deleted SQLite db table row.

Basically my program will delete a row when a certain activity is loaded, and I want to check the value inside the row.

This is my get code :

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

I've used these codes to delete the value :

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

public void deleteSlotSQL()
      {
          database.execSQL(ChosenSlotDatabaseHandler.DELETE_SLOT);
      }

And this is my condition to set a TextViewvalue :

if(slots == null)
            {
                chosenSlotView.setText("You have not parked");
            }
            else
            {
                chosenSlotView.setText("You are parked in : " + slots);
            }

At first, I thought that once the only row is deleted, getSlot() would return null, but it seems that it's not null from this Log.d I ran :

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

The log returns "not null dude"..

Any suggestion on how to get the slots value so I can set the TextView value??

2
  • First off all you should avoid using logs like "not null dude", you will find them funny and suggestive but after a while we won t be able to write clean and professional code . Commented Jun 12, 2015 at 7:30
  • 1
    Nahh, that log was only debug purpose, I was just checking it out. It's going to be deleted later or changed into a more professional sentence :) Commented Jun 12, 2015 at 7:33

2 Answers 2

1

your slots definitely not null because of this : String slots = new String();

it should be

 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;

EDIT :

Nevermind that String slot = null,

Try using :

if(slots.isEmpty())
        {
            chosenSlotView.setText(notParked);
        }
        else
        {
            chosenSlotView.setText(isParked + slots);
        }
Sign up to request clarification or add additional context in comments.

9 Comments

But inside if clause, there's slots = cursor.getString(0); How to obtain the value of getString(0)?
can you provide the ChosenSlotDatabaseHandler.TABLE_CHOSEN_PARKING_SLOT string define ?
It is chosen_parking_slot, could it be.. This is the culprit?
Tried directly using chosenSlotView.setText(slots); outside if, but it doesn't show any output
try chosenSlotView.setText("test text :" +slots); because slots could be an empty String, like "";
|
1

First off all you should avoid using logs like "not null dude", you will find them funny and suggestive but after a while we won t be able to write clean and professional code. My second advice is to use constants instead of hadcoded strings . Create a class Constants and add there strings

   public static final String NOT_PARKED = "You have not parked"

My third advice is to take a look at ormlite http://logic-explained.blogspot.ro/2011/12/using-ormlite-in-android-projects.html

If the logs are ok , maybe there is a problem with textview . Try putting a text in textview before the condition to check if will get set . Check the layout file also .

1 Comment

Ah yes, I've changed them into constants.. I thought that it'd be convenient for me, but yeah, modularity in mind, that was a wrong decision.. The TextView is okay, it still show value gained from here chosenSlotView.setText("You are parked in : " + slots);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.