How to save listview in android and fetch it back when required?
Asked Answered
R

3

7

I have a listview having three fields , but I don't know how to save my listview in android and fetch it back when user open app and display it.

I want to show the data saved in listview and I have to save the new data which user enter into my listview.

Rimskykorsakov answered 14/12, 2015 at 4:50 Comment(1)
use database instead ...Hound
F
1

As per your requirment, there is no direct way to store list view, but you can store the data in the list view and you can set it in to the ListView later, for that you have to use Sqlite Database, refer official documentation of sqlite here, also check out this example.

read this example too (ListView of Data from SQLiteDatabase )

Fecundate answered 14/12, 2015 at 4:57 Comment(0)
C
3

I would recommend having some sort of database on a server that would save that information and that you would need to pull that information from the database to display it in the app. However, android also has something called a sharedpref that allows you to save stuff in the app when the app is closed. The link is below. However this is not the best way a database would be, but it may work for your needs.

http://developer.android.com/reference/android/content/SharedPreferences.html

Cota answered 14/12, 2015 at 4:55 Comment(1)
If you try to do anything complicated with SharedPreferences quickly becomes very tedious; you can only store primitive types and it's a simple key/value system. So,SharedPreferences are meant for lightweight storage of simple data.Fecundate
F
1

As per your requirment, there is no direct way to store list view, but you can store the data in the list view and you can set it in to the ListView later, for that you have to use Sqlite Database, refer official documentation of sqlite here, also check out this example.

read this example too (ListView of Data from SQLiteDatabase )

Fecundate answered 14/12, 2015 at 4:57 Comment(0)
A
1

It depends on the purpose and the requirements of your app. If you are developing an offline app, then you should save the data entered in the listview.

Again, there are multiple ways to have the user enter the data. You could use a FAB(floating action bar) or have a text box in the last listview item.

Once the user has entered the data, you should save it to the sqlite database in a background thread and call notifyDatasetChanged() method on the listview adapter for the changes to reflect on the view.

If you have to send it across the network to store it in your server, you should do the networking on the background thread using a AysncTask or Handler thread model, or use libraries like volley or picasso to make it easy.

Hope this helped!

PS: Use ViewHolder pattern to improve performance in a ListView. Or there is RecyclerView from Android 5.0.

Aliphatic answered 14/12, 2015 at 6:10 Comment(2)
RecyclerView from Lollipop ?Fecundate
Updated answer. RecyclerView from 5.0(also available on V7 support library)Aliphatic

© 2022 - 2024 — McMap. All rights reserved.