Michael Michael - 3 months ago 87
Android Question

Change viewtype between listview and gridview and keep data Android

I have a working note-taking app currently using a listview. I want the user to be able to switch to a gridview. The switch is not the problem as I could just load a different layout file with the different view, but I want their current notes in the listview to be transferred over to the gridview notes upon the switch. Is there a way to access each notes contents using a for each loop and then add the contents to the gridview using something like

gridviewitem.add(listviewNoteContents);


The issue I'm seeing with this is how would I send the note data between the 2 views if I'm loading 2 layout files. The note contents are stored in the SQLite database, but I don't want to have to query the database everytime I switch views as it seems inefficient compared to just getting the note's contents and adding them to a note in a different view.

Answer

Something I have done before that I know works is to put your data into a RecyclerView and switch the layout manager between a LinearLayoutManager and a GridLayoutManager. The adapter doesn't change so the data isn't affected. The adapter can have different view types based on the current layout manager.