Xamarin Android – Using ListView Controls with Shared Preferences

I have previously demoed how to work with the ListView control on the Xamarin Android platform but this post is going to be a little more advanced.  If you are new and need to get started with the basics of the ListView control, I suggest you start with this post. Once you have the basics down, you can also check out how to make your own custom ListView Adapters in the two-part tutorial that starts with this post.

This tutorial also assumes you know how to use some of the built-in storage for your Android device. If you’re not sure how to access things like Shared Preferences, check out this tutorial first!

If you’re up to speed and ready to go, let’s check out some advanced ListView functions!

Setting up a ListView

I’ve shown you a couple of different ways in the past to setup a ListView but here is some review.  You can basically choose to either put a ListView control on the form (by getting it from the toolbox) or you can change your Activity type to a ListActivity.  A ListActivity is going to inherit a ListView control all on it’s own so you can just access the ListAdapter directly and provide whatever data items you want to show.

Shared Preferences

The Shared Preferences area of the Android system allows you to store data about your application as key/value pairs. The data you can store are all simple types such as bool, float, int, string and long. The data that you save in Shared Preferences is only for your app and it cannot be accessed by other apps on the device. However, the data is persistent which means it is saved to the phone and you can access it or modify it at any time from within your app. This makes for a handy place you can quickly store information that you want to use later.

Writing to Shared Preferences

Writing to the Shared Preferences data area is pretty simple.  We access it through an interface called ISharedPreferences. This interface requires the Android.Content using statement to be added to your project. When you first start using Shared Preferences you have to initialize a “file” for your app to store the key/value pairs inside.  It looks something like:

ISharedPreferences prefs = Application.Context.GetSharedPreferences("USER_DATA", FileCreationMode.Private);

Then you can setup your editor

ISharedPreferencesEditor editor = prefs.Edit();

and add in all of the key/value pairs you want

editor.PutString("item1", "Steak");

When you’re done, you apply the edits

editor.Apply();

Reading from Shared Preferences

Reading from the Shared Preferences file isn’t really any more difficult than writing to it. You use the GetString(), GetInt(), etc…methods that correspond to the types of data you have stored. By providing the right key name, you get your value returned. This will look something like:

prefs.GetString("item1", null);

I hope you have enjoying this quick tutorial on using ListViews with SharedPreferences! If you want to get a walk-through of how to put this together, check out the video on YouTube below.  You can also get some sample code from my Github: https://github.com/rhysma/XamarinAndroidListViewSP

Leave a Reply

Your email address will not be published. Required fields are marked *