Blogs

What I learned today, February 23, 2015 (mostly Android)

This is a collection of notes about what I learned today, February 23, 2015. Most of it is about Android.

I need to refresh my cursor data set before calling notifyDataSetChanged

When (a) adding, editing, or deleting items in a ListView and (b) using a CursorAdapter, I need to update my cursor object before calling notifyDataSetChanged. I created this method, which I call from my fragment’s onResume method:

Android/Java: How to get your app's root data directory

If you ever need to get the root data directory of your Android application (app) from within your Java code, I can confirm that this approach works:

File rootDataDir = getActivity().getFilesDir();

When I log that directory like this:

Log.i(TAG, rootDataDir.toString());

it prints this output for my application:

/data/data/com.alvinalexander.mynewapp/files

where com.alvinalexander.mynewapp is the package name for my new Android app.

How to get Android ActionBar menu item icons to show (without compatibility libraries)

I recently upgraded an Android application I’m working on to get away from all of the “application compatibility” stuff and only use Java classes that are not in the compatibility libraries. When I did this, all of my menu item icons disappeared from the action bars, and they appeared only as text in the overflow menus. Not cool.

In short, to fix this problem I just had to add this setting to my menu item definitions:

Source code for an Android AsyncTask (REST client) example

I just got back into using an Android AsyncTask, and it took me a little while to re-load the concepts in my head. I used AsyncTask’s a few years ago, but haven’t used them since.

To help remember how they work, I created a little AsyncTask example project, and I’ve included all of the source code for that project here. I’ll show all of the source code for my classes and configuration files, and then explain the code at the end.

Android: Generic methods to write/save Android preferences data

I was working with Android Preferences recently, and after writing a few “save my preference” methods, I decided to take a few minutes to organize my code and put all of those Preference methods in a PreferenceUtils class. I made the methods static, and following functional programming principles, I made their output depend only on their input, so they require you to pass in a Context reference, along with your preference key and value.

Syndicate content