Archive for November, 2009

Cruising on Android

Monday, November 23rd, 2009

All the pieces are falling like dominoes now. Java code itself is not so different from delphi’s object pascal. And the more I work with Eclipse, the more I like it. Coding the GUI workings was the toughest part, setting up all the listeners and adapters with all that verbose, confusing code. But now that most of that is out of the way, I could be working on the pk code this week… if I didn’t have to go back to heartland hell. I’m anxious to see it up on my phone, but it will have to wait for another week.

Finished up all the details on the patient data screen calculations, added routines to check if ht/wt appropriate for age, added multiple crcl methods, and added load preferences code.

Will code the prospective PK routines next, I don’t anticipate any problems there. The next big hurdle will be the 2d graphing functions, I’ve purposely avoided reading anything about that yet.

A picture of the completed model screen:abpk_droid_model

Android Week 4

Saturday, November 21st, 2009

Call this week 4, even though I started this project on Sept 22, I only work on it every other week.

Finally starting to take shape. Coded a simplified CrCl routine, simply copied the Pascal code from my Delphi app, since Pascal syntax is very similar to Java, ran without any mods.

Coded the main menu and preferences dialog. Found the Android preference routines straightforward and less verbose than most other basic tasks in Android.

Had to add a blank OnResume routine so Android would (automatically) restore the current state. However, tapping the back key to exit out of the app erases the state, so will need to code a savestate routine.

Android has no native XML parsing support and SQlite is overkill, so I decided to use JSON for saving and passing current session data. Found JSON to be easy to use and perfect for data exchange.

Discovered how to pass data from one screen (Java:intent) to another:
// Declare new Intent
Intent i = new Intent(Main.this, ViewModel.class);
// Pass bundled data as JSON string
i.putExtra(“Main.term”, CreateDataString());
// Show new screen
startActivity(i);

Then, in the OnCreate of the receiving screen:
// Get passed JSON variable
Bundle extras = getIntent().getExtras();
DataPassed = extras.getString(“Main.term”);

Using the JSONStringer function to create the passed data string:
private String CreateDataString(){
// Declare variable
String dataString=”";
// Create dataString
try {
dataString = new JSONStringer()
.object()
// Model data
.key(“ModelName”).value(thismodel.getDrug())
.key(“ModelVd”).value(thismodel.getVD())
// etc
// Patient data
.key(“Age”).value(txtAge.getText().toString())
// etc
.endObject()
.toString();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dataString;
}

Then in the Model screen, using a JSONobject to parse the passed string and populate the default parameters on the Model view screen.
try {
JSONObject jo = new JSONObject(DataPassed);
String strTemp = jo.getString(“ModelName”) + ” model parameters”;
lblTitle.setText(strTemp);
double dblTemp = jo.getDouble(“ModelVd”);
txtVd.setText(String.format(“%.2f”, dblTemp));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

So, with that I’ve completed the basic skeleton and flow of the first two parts of the program. Now the grunt work to fill in all the details.

Starting to see some light at the end of the tunnel.

Toasting Android

Thursday, November 19th, 2009

So the last two days haven’t been quite so easy. The amount of code required to do the simplest things in the Android GUI is ridiculous. For every widget you first have to declare it. Then declare access to the widget by it’s XML id in R.java. Then you add a listener for each widget. And finally, you code each listener.

Setting up listeners have been a chore. Spinners and RadioButtons don’t support clicks. With a spinner you set an OnItemSelected listener. With RadioButtons, intead of coding each click, you set an OnCheckedChange listener for the Radio group, a more logical approach.

The Android dev docs are thorough, but have very few examples, thank goodness for google. I never would have figured out that to call another screen you first declare an Intent, then call startActivity:
Intent i = new Intent(CurrentScreen.this,.. NewScreen.class);
startActivity(i);

I’ve yet to get a dialog to display, but Android does have a cool timed popup called (appropriately) Toast. Much more user friendly than those horrible modal dialogs in Windoze. So instead of this insane amount of code for a simple model dialog (which I can’t get to display anyway):
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage(“Patie..nt data cannot be blank”);
dlgAlert.setTitle(“AbPK”);
dlgAlert.setPositiveButton..(“OK”, null);
dlgAlert.setCancelable(tru..e);
dlgAlert.create().show();

You pop in a piece of toast:
Toast.makeText(context, “All patient data must be entered”, Toast.LENGTH_SHORT).show()..;

I love it.

Android Day ??

Wednesday, November 18th, 2009

After two weeks of beating my head against a concrete wall, a break through of sorts. I ditched the overly complex code from “Android Programming Tutorials”, and replaced it with the SimpleCursorAdapter from Android dev docs. It worked like a charm. Now I can populate a spinner with drug names from the drug model database.

I suppose Murphy was also trying to teach Java OOP by using extended classes to create wrappers and adapters for something that only took 6 lines of code. And such is the problem with all of these how-to books. Nearly every programming book that I have ever read leads you all the way around the subject, without ever actually showing you how to do anything practical. I even posted sample code on his discussion group, he had no more clue than I did, and basically told me to go away.

Anyway, up and running again, now for the fun part, coding the calculation routines.
abpk_android