Sharing some of the useful snippets of code i stumble across with the world. It will mostly be Android, cloud computing, ColdFusion, SQL, Amazon AWS and other web technologies.
If you like what you read or it helps, drop in a comment and say so, it will be appreciated.

15 August 2013

If you add a standard action bar to your Android project, you're forced to upgrade your minimum SDK version to 3.0, (API level 11 Honeycomb). This was when Google added the action bar. However if you want to support older versions of Android in your app then you can now use Compat. This enables you to drop back to Android 2.1 Eclair (API level 7).

Figuring out Action Bar Compat is not the easiest of tasks and requires quite a few steps.

Action Bar Compat runs off a separate library that you need to install in your eclipse / studio workspace. Follow the steps in the above link carefully. You'll want to use the drop down "Adding libraries with resources" and follow those instructions. Once you're done you should have a new project named android-support-v7-appcompat in the workspace with no errors.
I had to modify my project.properties file to use the latest sdk:

target=android-18

Your Project

Now you can obviously create a new project or use an existing one. You'll want to make sure you right click on your project, click properties, click Android and then verify / change the following things:

You need to add the android-support-v7-appcompat project as a library.

You need to make sure the project build target is the same for your project as it is for android-support-v7-appcompat. This is currently Android 4.3.

Now you can start changing the contents of the project. First the res/menu/menu.xml file has a couple of small changes:

08 August 2013

Android listViews are pretty useful and pretty simple. You have to get your head around some sort of adapter, but that's no huge issue.

However I've previously never bothered to find out how to get more than one column on a listView. Luckily this is much easier than I first thought. My example uses contentProvidors to setup the cursor, but I think this should work with DB stuff as well. Given a bit of hocus pocus.

Now move to the MainActivity or wherever your display function will be and you'll need something like this:

private void outputCursor(){
// Defines a list of columns which we have defined in our cursor, and will be output to listView
String[] mWordListColumns =
{
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY
};
// Defines a list of View IDs. These are IDs of textViews defined in a serperate layout file
int[] mWordListItems = { R.id.name_entry, R.id.number_entry};
// Creates a new SimpleCursorAdapter
SimpleCursorAdapter mCursorAdapter = new SimpleCursorAdapter(
getApplicationContext(), // The application's Context object
R.layout.contacts_single, // A layout in XML for one row in the ListView
mCursor, // The result from the query
mWordListColumns, // A string array of column names in the cursor
mWordListItems, // An integer array of view IDs in the row layout
0 // Flags (usually none are needed)
);
// Sets the adapter for the ListView
ListView myListView = (ListView) findViewById(R.id.listView1);
myListView.setAdapter(mCursorAdapter);
}

That should be all you need. Note the mWordListItems is passed into the adapter and the R.layout.contacts_single reference, telling the adapter to use these custom elements to display its contents. Then we get the ListView from the original activity and set the adapter.
Simples!

06 August 2013

SQL Injection is one of my favourite topics, sad but true. I think SQL injection is a very clever way of deceiving apps. This then encourages developers to be equally clever and thorough in their jobs to protect their data. I'm not for a moment endorsing such practices, what I'm saying is good developers should be aware of the possibilities.

We'd have a problem. Now fortunately database.execSQL() doesn't allow us to run more than one sql command in one execution.
From developer.android.com:

Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.

In some ways this limits the potential of SQL injection, but we don't want to rely on that and it is horribly bad practice. Lets try it on our app and see what happens, paste the above (drop table statement) into the pop-up that appears when you click the btnFuzzy. You'll see your app crash and a SQLException in logCat.

Now you'll see we've hardly changed anything, we use contentValues to store a set of values, the column name and the value. Then we insert that with database.insert. Super simple. If we re-run this and try our SQL injection trick, no more critical error :)