Move items up or down in listview

I have a list that I want to be able to re organize the items in the list. I searched and found info on moving items up or down with 2 buttons, one for up and one for down to move the selected item but i am having a problem with it erroring out if you reach the beginning or end and try to move it again. like if you move a item down to the bottom of the list and then click down again it crashes. Is there a better way to do this or should I just try to do some sort of message box saying you have reached the end and back up one place? This is what I have now

Welcome John.

No, better to write code to prevent the List from ever getting to less than one item. Something like:
if length list is 1 then do not delete
perhaps is an alternative. Experiment.

You may also find this extension useful

offers a long press option which may help avoid using the two buttons....

Thanks. I will do some more playing

See Show checkboxes in my text box - #6 by ABG

Simple code on my website:
https://www.professorcad.co.uk/appinventorsnippets#MoveListViewItem

This works great with just a list but i am having trouble when it is connected to a TinnyDB. I still have some stuff to figure out but this is allot of help. Thank you.

If you are saving items under different tags in TinyDB, they are getting sorted into alphabetic order.

I am just storing and retrieving the Value of the listview elements as what ever was typed in. I have not had a chance to play with it anymore yet

I can get it to work perfect without a TinyDB where you can add and delete and move items in the list but I get this error when I try to move items with the app connected to a TinyDB. I can add and delete items no problem just trying to move them errors

Screenshot 2023-10-02 180813

Show your blocks now....

use Do it to debug your blocks, see also tip 4 here App Inventor: How to Learn | Pura Vida Apps
see also Live Development, Testing, and Debugging Tools
Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by icon24 Taifun.

I got it to move with no error but it only loads 1 line to the list from the DB on start up and adds 2 lines when I add a item, 1 with the item and one empty. I am uploading 2 sets of blocks. the first one works perfect with not being able to move the items. The second one is the one I got to move but messed something up with loading the list from the database and adding new items to the list. Neither one gives errors now.


You have three different copies of your list:

  • one in TinyDB
  • one in a global variable
  • one in the ListView Elements

You should regard the TinyDB value as the copy of record, and be sure to use it as the basis for your operations (refreshing the ListView and global variables), and have it updated immediately after any change to that global list.

I have re done everything multiple times. The problem is in moving items that are in the database. I can build a app that adds and deletes and moves items in a listview no problem but as soon as I connect it to a database it crashes if I try to move a item up or down

Did you follow the recommendation by @ABG ?

You might want to provide an updated screenshot
of your relevant blocks for those, who don't want to check your complete project...

Taifun

(Canned Reply: ABG- Export & Upload .aia)
Export your .aia file and upload it here.
export_and_upload_aia

I have tried to follow all advice but keep coming up with the same conclusion. Thank you for all the help I have received so far. Here is my latest ,aia file & Thanks again.
MyList4.aia (28.7 KB)

You were confusing your item list with its TinyDB tag.
Here's my fix, using only local variables for the item list, derived solely from the TinyDB value, and updating the ListView Elements solely from the local item list.


I tested with text input, and reversed the up/down buttons to take into account that visual up means index down in a ListView.

I did not try voice input, because my voice sounds like a bowl of chicken soup.

1 Like

WOW!
Thank you so very much. That works perfectly. I now see where I went wrong.