Short source code examples

Without much discussion, here’s an Android ListView/ListFragment with its Back/Up/Home button enabled:

An Android ListFragment/ListView with Back/Up button enabled

(That button used to be a Home button, but now it’s used for the Back/Up action.)

And here’s the source code for that ListView/ListFragment:

Here’s an example of how to populate an Android ListView, where I get the data for the ListView from a database Cursor:

DatabaseHelper.TeamsCursor tc = DatabaseManager.get(getActivity()).getAllTeams();
ArrayList<String> listOfTeamNames = new ArrayList<>();
for(tc.moveToFirst(); !tc.isAfterLast(); tc.moveToNext()) {
    listOfTeamNames.add(tc.getTeam().teamName);
}

// list the team names with an adapter that talks to our listview
TeamNamesAdapter adapter = new TeamNamesAdapter(listOfTeamNames);
setListAdapter(adapter);

There are more formal ways to create an adapter class to work with a Cursor, but for my needs I just needed to get a list of names from a SQLite database table and show them in a simple ListView — part of a ListFragment — and this was the simplest code to write.

FWIW, the example also shows one way to iterate over the elements in a Cursor using a Java for loop.

As a quick note, if you need to get an Android TableRow to align right, I can confirm that this layout code works:

As a quick note, this source code shows how to set the data (array) on an Android Spinner from Java code:

Here’s an Android SQLite class I use in an Android app I wrote in 2014-2015. I’m sharing it here so I can easily find an Android SQLiteOpenHelper example:

I just learned an easy way to populate/initialize a Java int array with data, such as a range of numbers. The key is to use the rangeClosed method on the Java 8 IntStream class. Here’s an example using the Scala REPL:

scala> val n = java.util.stream.IntStream.rangeClosed(0, 10).toArray()
n: Array[Int] = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

I show that in Scala to show the output, and here’s what it looks like with Java:

As a quick note, this is how you create a Scala ArrayBuffer:

import scala.collection.mutable.ArrayBuffer

var fruits = ArrayBuffer[String]()
var ints = ArrayBuffer[Int]()

The key there is that the keyword new is not required before the ArrayBuffer.

While I’m in the neighborhood, here are some other ways you can work with ArrayBuffer:

As a quick note, I use code like this read a text file into an Array, List, or Seq using Scala:

def readFile(filename: String): Seq[String] = {
    val bufferedSource = io.Source.fromFile(filename)
    val lines = (for (line <- bufferedSource.getLines()) yield line).toList
    bufferedSource.close
    lines
}

As a short “note to self,” I just used this Scala code to (a) create a list that contains random strings of different lengths, then (b) shuffle the list of strings to create a more random effect:

This is a Bash shell script written for Ubuntu (Linux). I just switched from Mac/MacOS to Ubuntu, and I don't like the default blank screensaver in Ubuntu. I just want a screensaver to rotate my collection of images, so I'm considering using this rather than Xscreensaver. The script comes from jamcnaughton.com.

As a quick note today, to shuffle a list in Scala, use this technique:

scala.util.Random.shuffle(List(1,2,3,4))

Here’s what this approach looks like in the Scala REPL:

As a quick note, I haven’t tried to log into one of my GoDaddy websites in several months, and when I tried to log in just now I got this macOS ssh error message:

Unable to negotiate with <ip-address here> port 22: no matching host key type found. Their offer: ssh-dss

I have a shared web hosting account on GoDaddy, and wanted to do a 301 redirect with an Apache .htaccess file. For some unknown reason GoDaddy’s web interface wasn’t working for this, so I thought I’d fix the problem manually.

In short, this did NOT work:

Redirect 301 /the-old-uri  http://alvinalexander.com/the-new-uri

Here’s some source code for a little Scala email client. It’s nothing much, just a little wrapper that works with the JavaMail API:

As a quick note, if you need to see how to write a Scala method that returns a Future, this example may help:

I generally try to avoid this coding style these days, but, if you want to see how to use a Java BufferedReader and its readLine method in a Scala while loop, here you go:

I was looking for a good way to access XML resources (like RSS feeds) in Scala, and I currently like the idea of using ScalaJ-HTTP to access the URL and download the XML content, and then using the Scala XML library to process the XML string I download from the URL.

This example Scala program shows my current approach:

Note: The code shown below is a bit old. If you want to perform a “search and replace” operation on all instances of a given pattern, all you have to do these days is use the replaceAll method on a Java String, like this:

String s = "123 Main Street";
String result = s.replaceAll("[0-9]", "-");

That second line of code returns the string “--- Main Street”. I kept the information below here for background information.

Using Akka logging is a great thing, until you need to turn it off. In short, to disable Akka logging, you need to create a file named application.conf in your SBT src/main/resources folder, and set the loglevel to “OFF” in that file, like this:

As a quick note, if you ever want to created a dotted border that has some RGB opacity to it, I just used the following CSS code to style some hyperlinks, and I can confirm that it works: