-1

i got a problem with SQLite tables and a listview. I have implemented a db including tables and data and can display them in a listview. I want to edit the entries which is working well too. Only the ordering isn't as good as i want it.

When i want to show my entries ordered by column 1 or column 2 and edit the entry after that, i have the problem, that i used the position in the list for the position in the table (with cursor.moveToNext() for x times).

Now i solved it with reading out the whole table with

SELECT * FROM table ORDER BY column

Deleting and refilling the table with the ordered data. Then read out again and display it.

But this isn't a good neither a secure way to do it. If the app crashes while it's doing some work i could lose data. Is there a good way to get a reference where i have to edit in the listview without displaying it? Or should i work with custom ListView-layouts and an extra data class for the listview so i don't have to edit the whole db, just edit the data and after editing i rewrite it to the point in the database?

I didn't find a function for something like that in the SQLite doc. But maybe i didn't looked deep enough.

Thanks!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ian
  • 147
  • 1
  • 13
  • `Deleting and refilling the table with the ordered data` **... WHY?!** Keep using `ORDER BY`, instead. That's the correct way to retrieve ordered data from a database. – Phantômaxx Dec 18 '16 at 21:30
  • I want to get ordered data, yes. I want to show it in the listview. When i use the position of the listviewitem as reference for my db entry i don't get the correct entry for editing. The question was how to get a reference value which is independent from tht listview itemposition. – Ian Dec 19 '16 at 08:59
  • This is because you think that the items in the ListView are connected to the ids in the table. Which is **not**. Imagine if you remove a record with id=4. You now have records with id=0,1,2,3,5,6... – Phantômaxx Dec 19 '16 at 09:04
  • Yes i know.. That's why i changed mymethod to this horrible way with deleting. I don't know how to reference the right row with the right listview-position – Ian Dec 19 '16 at 09:07

1 Answers1

1

You shouldn't delete the whole database just to fill it again. This seems like an awful waste of time reading and deleting, over and over.

You should keep track of the id, belonging to the item you wish to edit. Once you are done editing update the specific item in the database, by referencing it with the id.

Im not sure, what it is that stands in the "column" column. But if you wish to sort the table in 2 ways, you could either provide another column with the attribute to sort them by, or change the order by using ascending/descending when ordering.

Daniel Bo
  • 2,518
  • 1
  • 18
  • 29
  • How do i keep track of the id? The function for reading the table returns `List`. I don't want to display the id. Do i have to use a data class for storing the table data and get a object of it or should i use an array and build a list out of the array values? From `array[0][0]` to `array[n][m]` ? N represents the row number, m the number of the data field of the row. – Ian Dec 19 '16 at 09:05
  • I would probably use tags on the individual rows. check http://stackoverflow.com/questions/5291726/what-is-the-main-purpose-of-settag-gettag-methods-of-view If you do that, you can "ask" the row for the id, which belongs to the item represented in that row. basically row.getTag would return the id of the element in the database. – Daniel Bo Dec 19 '16 at 09:29