Remove a result, print a new one in its place

So I have a button that when pressed runs and gets one random row from a database.

This works fine. Now, how do I get rid of the result from the display, and print a new result when the button is pressed again?

Ive tried placing display.remove in a number of places but cant quite seem to figure it out. Currently pressing the buttton just places a new result on top of the old one.

local function page(event)

for row in db:nrows(“SELECT name from database ORDER BY RANDOM() LIMIT 1”) do

local result = row.names
 
local text = display.newText(result, 50, 120, native.systemFont, 16)
  text:setFillColor(1,0,1)
 
end

end

local button = display.newImage(“icon.png”)
button.x = 225
button.y = 250  
button:addEventListener(“tap”, page)

Pointers to how to do this are welcome.

Well, my guess: You’re creating new text on top of each other. Maybe if you just declare your variable outside of the for loop, and uptade her as clicking the button.

local text = display.newText("Just a test", 50, 120, native.systemFont, 16) text:setFillColor(1,0,1) local function page(event) for row in db:nrows("SELECT name from database ORDER BY RANDOM() LIMIT 1") do local result = row.names text.text = result end end local button = display.newImage("icon.png") button.x = 225 button.y = 250 button:addEventListener("tap", page)

I went ahead tried that, it did not go through. Its hard to access the items in the loop outside of it, which makes sense I suppose.

Just for test, try to remove the “local” of the text variable. Will make the variable a global.

Like this:

text = display.newText(“Just a test”, 50, 120, native.systemFont, 16)
text:setFillColor(1,0,1)

Hi @Betlic,

The code that @Ziad.Ali posted looks good overall, so I don’t see any reason why your text is not updating. Can you add a simple print() statement to see what the value of “result” is from the database? Is this just one string value, or is it pulling out a whole table/array?

Brent

Its pulling out one string. The code above does work, but Im still not sure how to get rid of the previous text on the screen when the button is pressed again, instead of just piling the text on top of each other.

Hi Betlic,

If the text is “piling up” then there’s an object reference issue, where it’s not recognizing the previous object, and thus it creates a new one. Updating the .text property on a text object should update that text, so if that’s not occurring, then you need to carefully check your references to see why it’s not updating the one you want.

Brent

I tried your code again Ziad, its working. Your right, by placing the result out of the loop by doing this,

text.text = result

Lets it update properly. Just so I understand, could you explain text.text? I know one of the texts is a variable, and I know two are needed for it to work, but am not sure how the two togehter let this work.

Still, much obliged.

Hi Betlic,

In “text.text”, the first is your variable name. That could be anything that isn’t reserved or “illegal” as regarded by Lua, i.e.

myText.text

someOtherName.text

etc.

The second (.text) is a property of that object, used by Corona, and it must be .text, because that specifies the object’s text (how that text object visually appears). Changing this property updates the text object.

Brent

Actually, this guide explains it all better:

http://docs.coronalabs.com/guide/start/helloWorld/index.html

Thanks for both of your helps.

If your still around Ziad.Ali, what if I wanted more then 1 result out of the loop? I noticed it only lets me return one result. Would it make sense to go in the direction of using * row.id inside the loop to produce more then one result? I would already have removed LIMIT 1 of course.

Wassup?

What do you mean? When “tap” happens you want to show more than one row? If so, how? In the same display.newText, concatenating the result string? Creating new texts each row?

Thats right, I would like to show more then one result. Would looking at the widget for tableview be going in the right direction?

Yes, it could be an option, i guess.

Have you watched this video?

https://www.youtube.com/watch?v=PXo4Rv5KPjo

Im not sure, Ill watch that again, thanks.

Well, my guess: You’re creating new text on top of each other. Maybe if you just declare your variable outside of the for loop, and uptade her as clicking the button.

local text = display.newText("Just a test", 50, 120, native.systemFont, 16) text:setFillColor(1,0,1) local function page(event) for row in db:nrows("SELECT name from database ORDER BY RANDOM() LIMIT 1") do local result = row.names text.text = result end end local button = display.newImage("icon.png") button.x = 225 button.y = 250 button:addEventListener("tap", page)

I went ahead tried that, it did not go through. Its hard to access the items in the loop outside of it, which makes sense I suppose.

Just for test, try to remove the “local” of the text variable. Will make the variable a global.

Like this:

text = display.newText(“Just a test”, 50, 120, native.systemFont, 16)
text:setFillColor(1,0,1)

Hi @Betlic,

The code that @Ziad.Ali posted looks good overall, so I don’t see any reason why your text is not updating. Can you add a simple print() statement to see what the value of “result” is from the database? Is this just one string value, or is it pulling out a whole table/array?

Brent

Its pulling out one string. The code above does work, but Im still not sure how to get rid of the previous text on the screen when the button is pressed again, instead of just piling the text on top of each other.