Using simple_list_item_2 with an ArrayAdapter (Android)

Today I wanted to display static a ListView of properties and their values. It’s Friday, so I really wanted to do this with as little work as possible. Android’s free layout simple_list_item_2 sounded like what I wanted, but it wasn’t immediately clear how to handle this. There are a lot of lazy ways we can list single values, but Googling around how to use simple_list_item_2 resulted in lots of questions with no real answer.

I found Mark Assad, who parsed the system layout files into raw XML. The led to the magic answer of TwoLineListItem widget. Finding this widget allowed me to write a simple ArrayAdapter of key/value pairs.

adapter = new ArrayAdapter<BasicNameValuePair>(this,android.R.layout.simple_list_item_2,list){

        @Override

        public View getView(int position, View convertView, ViewGroup parent){

            TwoLineListItem row;            

            if(convertView == null){

                LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                row = (TwoLineListItem)inflater.inflate(android.R.layout.simple_list_item_2, null);                    

            }else{

                row = (TwoLineListItem)convertView;

            }

            BasicNameValuePair data = list.get(position);

            row.getText1().setText(data.getName());

            row.getText2().setText(data.getValue());

            

            return row;

        }

    };

listView.setAdapter(adapter);

 

Hope this saves someone else some time, as I spent an embarrassing amount of time determined to use the simple_list_item_2 layout.

A Bit Scatterbrained…

Lately I find myself mixing my PascalCase with my camelCase, calling alloc init instead of new, using [ ] instead of (), and writing awfully verbose method names all over the place. I justify my errors with a glimpse at my average day here lately:

Not to scale. Some margin of error. I focus on my family all day. I promise!

The Value of Documentation

Having been reared as a developer in Microsoft’s cozy little .NET world, I didn’t really appreciate the value of quality documentation until I ventured out into the wild jungle.

I’ve been using Appcelerator’s Titanium Mobile SDK for the past few months. It’s kind of become my Shere Khan here in the jungle. Theoretically, using Titanium Mobile is easier and faster than writing native code. This might be true if I didn’t waste so much time deciphering their documentation.

I made a button to toggle my table view’s editable status. The object had a property called editable, so that seemed to be what I wanted. After testing, however, all this did was toggle whether or not swipe-to-delete worked. What I really wanted was the little red minus sign to appear, and have the contents of each row shifted to the right to make room. After 30 minutes of trial and error, looking through TableViewRow properties, and searching their Q&A section (because Appcelerator has no official forums) I found that TableView actually has two properties: editable and editing.

 

image

 

Even after discovering this other property, I still had to actually test each one to see what they did. Apparently, “editable” toggles the swipe-to-delete, whereas “editing” toggles the minus-button-on-the-left thing. The two are independent of each other.

Thanks for saving me time, web based application framework!

Signed,

Bitter in Bossier

Enterprise Library Sometimes Chops XML into 2033 Characters

This morning I began what I thought would be an uneventful task in .NET: connect to one database, download as XML some rows from a table that have changed since a given time, then pass that XML to a stored procedure in another database so that the changes can be merged. Just two tables: one relatively small, the other just a 2-column relationship table for the first table. Here’s a contrived snippet to serve the same purpose:

The SQL:

CREATE PROCEDURE dbo.GetXmlValue

AS

SELECT TOP 100
 *
 FROM sys.all_columns
 FOR XML AUTO, ROOT('Root')

The Code:

Database db = GetDatabase();

DataSet set;

using (DbCommand cmd = db.GetStoredProcCommand("dbo.GetXmlValue"))
 {
     set = db.ExecuteDataSet(cmd);
 }

In my application, it was this second table that tripped me a bit. The first only had a few changed rows, so the XML was only 500-1000 characters. The second table generated a much longer XML string – 3864 characters long. My initial plan was to return two tables, each 1 column and 1 row. When using Database.ExecuteDataSet to fetch the results, however, the second table yielded two rows. I backtracked, second guessed myself, and even grabbed a couple of guys in the hall to double check my code. No one could explain it. It seemed odd that this hasn’t come up before for someone here. I did a bit of Googling and only found a couple of results. This Microsoft Knowledgebase Article just says it’s “by design” and to use SQLCommand.ExecuteXmlReader.

The next question that came up was “Well, what does it do if I return a long XML value among other values in the same row?” I tested this out and everything worked as expected. All of the XML was in a single column value as it should be.

I still can’t exactly explain it. ExecuteScalar has a character limit of 2033 characters. I suspect that because each table in my result set is just a single column value, something somewhere is relying on ExecuteScalar.

My solution? In my stored procedure I just stored each of my table results as an XML typed variable, then returned the two together in the same row.

GTIN 12 UPC Check Digit Calculation in SQL Server 2008

I recently needed to generate the check digit for a GTIN-12 UPC code in SQL. The calculation method for the check digit varies depending on what kind of barcode you’re implementing, so I popped on over to Google to get the necessary algorithm. Naturally, and regardless of simplicity, I checked for any available code snippets. The only I found weren’t GTIN-12, and were in C or C++. So for anyone looking to do it (or something similar) in T-SQL, here’s a scalar function to do just that.

CREATE FUNCTION dbo.CalculateBarcodeGTIN12CheckDigit(@input CHAR(11))
RETURNS INT
AS
BEGIN
	DECLARE @evenDigitSum INT = 0
		,@oddDigitSum INT = 0
		,@i TINYINT = 0
		,@result INT;

	-- check if given Barcode is Numeric , if not return error status -1
	IF(ISNUMERIC(@input) = 0
		OR LEN(RTRIM(LTRIM(@input))) != 11)
		RETURN -1

	-- start the compute BarCode checksum algorithm
	SET @i = 1
	WHILE (@i <= 11)
		BEGIN
		 --Add odd and even digits separately;
		 IF((@i % 2) = 0)
			 SET @evenDigitSum += CONVERT(TINYINT, SUBSTRING(@input,@i,1))
		 ELSE
			 SET @oddDigitSum += CONVERT(TINYINT, SUBSTRING(@input,@i,1))
		 SET @i += 1
		END

	--As per: http://en.wikipedia.org/wiki/Universal_Product_Code
	--Multiply odd sum by 3, add to even sum, and mod 10.
	SET @result = ((@oddDigitSum * 3) + @evenDigitSum) % 10;
	IF(@result = 0)
		RETURN 0
	ELSE
		RETURN 10 - @result;

	RETURN -1
END
GO

Scalar functions are killer, but I’ll only ever be using it for a single record.

Hatcher William

20110520-104025.jpg

So. Awesome. :)

Berlin – All Panoramas

Because I didn’t want to include all of the panoramas I took in my main Berlin summary post, here is a list of nearly all of them for those that care:

Apartment:
http://www.occip.it/pyh2l87gj

Park behind apartment:
http://www.occip.it/pyh30sy9j
http://www.occip.it/pyh80z5ij

Nazi war crimes trial exhibit:
http://www.occip.it/pyh405w8j
http://www.occip.it/pyhsssgj

Outside the Nazi war crimes courthouse:
http://www.occip.it/pyh7em0j
http://www.occip.it/pyh20eo5j

Outside our Nuremburg hotel:
http://www.occip.it/pyh78bnj

Church in Nuremburg (half):
http://www.occip.it/pygy00nksj

Bridge in Nuremburg:
http://www.occip.it/pyh203r7j
http://www.occip.it/pyh40yluj

Medieval Dungeon:
http://www.occip.it/pyhsunvj

Christmas markets:
http://www.occip.it/pyhsec8j
http://www.occip.it/pyh30wfjj
http://www.occip.it/pygy0t15j
http://www.occip.it/pyh30sfbj

Random Subway Station:
http://www.occip.it/pygz0cvgj

Main train station (Berlin Hauptbahnhof)
http://www.occip.it/pyhs0jsxj

Church:
http://www.occip.it/pyh30zyej
http://www.occip.it/pyh3006qxj
Outside: http://www.occip.it/pygy0v67j

Crypt:
http://www.occip.it/pyh50u4zj

Charlottenburg Palace:
http://www.occip.it/pyh69zfj

Holocaust Memorial:
http://www.occip.it/pyh80z4rj
http://www.occip.it/pyh3066kj
http://www.occip.it/pyh7i82j

Brandenberg Gate:
http://www.occip.it/pyh30dfsj — kind of messed up

Tiergarten:
http://www.occip.it/pyhsvm2j
http://www.occip.it/pygy0fraj

KaDeWe Restaraunt:
http://www.occip.it/pygz0ygrj

Gedächtniskirche:
http://www.occip.it/pyh80f95j

Berlin

Thanks to everyone that enabled me to take this trip – especially my wife Jen, who was selfless enough to allow me to take this trip without her.

This is the only car the communists had - and it's busting through the wall. Genius.East Side Gallery

Berlin Hauptbahnhof – the largest train station in Europe. It’s quite impressive in its size and feels much more like an airport.

I ditched the turtle-looking hat after a while. It was crawling with cops carrying submachine guns.

Tiergarten – the park just in front of the Brandenberg gate. The parks are stunning when covered with fresh smooth powder like they were while I was there. The scenery was just as foreign to me as the culture. Here you see the first glimpse of how I promised Jen she would be with us:

She was there in spirit. I should've made one for my son too. Just 3 studs... creeping through the park.

Brandenburg Gate – Rain or shine, I think there are always people around this gate.

A real Christmas tree!IMG_0283

Denkmal für die ermordeten Juden Europas (Memorial to the Murdered Jews of Europe) – Walking through this place made you very uneasy. It was a bit disorienting at times while in the middle of it.

It went on forever The size was enormous. It's impossible not to want to play in it.

Christkindlmärkte (Christmas Market) – There were a lot of Christmas markets. We attended quite a few in Berlin and the famous large market in Nuremburg. These were a lot of fun. I ate lots of sausages and breads, the best hot chocolate in the world, guaranteed, and struggled to find a piece of chocolate that wasn’t loaded with rum.

Best hot chocolate. In the world. Ever. They always give you real cups and plates and cutlery and you are refunded a few dollars when you return them. CREPES WITH NUTELLA! AHHHHH! SOOO GOOD! Charlottenburg Palace NOM NOM NOM Nuremburg's Christmas market was packed Warm wine - DELICIOUS in cold weather! And kind of potent... Tasty looking dried fruits - I got sausage and crepes instead.The Mother of Hot Chocolate

das Essen – Food was a big target of mine while there. I hear Germans “only eat meat and potatoes” and frankly that generalization isn’t too far off the mark. My ideal cuisine!

A big wad of pork. And potatos. Schnitzel! The best beers in the world! Best (only?) brunch I have ever had! Brunch buffet - twice as long as this. And there were two. I could only identify one thing on this plate.Their beers are much larger than ours.  *hiccup* "Ghetto Burger" - yes, it came with a shot of Vodka. French Fry Sauce (this was in Amsterdam)

die Geschichte – Spending time in a country with as much history as Germany is a clear reminder of how young the United States really is.

The scale of this church is hard to capture. This is no Southern Baptist church... Most of this church was destroyed during the war. Dead Prussian royalty in the crypt under a church. Wedding is the name of the neighborhood. Nazi War Criminals were tried here.

Lochgefängnisse – While in Nuremburg, we toured a medieval dungeon under what was once a monastery, then became City Hall in the mid 1300’s. This was genuinely the creepiest place I’ve been to in a while. It might’ve had little to do with the dungeon and more to do with the lady giving the tour only in German that sounded like she smoked twelve packs a day since she was 2.

She was a scary broad. Very cold - very low ceilings. The top of the door was at my throat level. That stone was hung from prisoners' legs. Window up to ground level. Leg stocks. This was the welcoming device - fingers were first crushed in it. The executioner's apartment room - can't explain this light. It happened multiple times. Some of the torture devices - the explanations were gruesome.

There were too many paintings  to photograph at the East Side Gallery, but I took some shots of a few remarkable ones:

The paintings go on seemingly forever Yes, that is Scrooge McDuck. They call him Dagobert Duck. I have no idea what's going on here.

A quick list of impressions this trip has left on me:

  1. I’d like to live in a big city some day.
  2. Public transportation is abysmal in most of the United States.
  3. Shreveport is a very dangerous city compared to Berlin.
  4. Die Erfahrungen sind wertvoller als Besitz.

An opportunity like this has made a much greater impression on me than anything I’ve got sitting on my shelf or in a closet. Thanks to my friends that had me along. Thanks to Mr. and Mrs. Hayes for sharing their son with us while they were visiting. Most of all, thanks to my wife for encouraging and enabling me to take this trip.

DSC01950

Thanks, friends (including Ben and Elena, not pictured!)

NOTE: I’ve linked to all of the panoramas I took in a separate post here: Berlin – All Panoramas.

360 Panorama – Occip.It View After Saving

Per the comment thread on Occipital’s blog:

This took me a while to find. As I’m in Berlin right now, my phone doesn’t work. I’ve pulled the SIM and I am just using it like an iPod touch. This has been a frustrating experience with regard to 360 Panorama because I rarely have an internet connection at the time that I take the panorama. All I can do is “Save”, and not having it in their nifty “Immersively stunning way” is a bit of a letdown.

The good news is twofold: They’re going to address it in an update AND there’s a way to manually recreate them with your saved image. First, upload them to www.yfrog.com. Luckily, they allow batch uploads. So if you’re like me and took 52 of them, it’s not too much trouble to upload them all.

Then the tedious part: making a list of all of the URLs created by the newly uploaded images. Once you make a list, just replace “yfrog.com/” with  ”occip.it/py” and voila! you have an Occipital magic view of your 360 panorama after saving it.

If anyone has any quick way of creating a list of all of your yfrog images, I’d love to know.

It’s not an elegant solution, but it should work until they update the app.

UPDATE: See comment for some of mine from my trip.

UPDATE 2: Checked for an app store update the very next day and it looks like they’ve addressed the problem.

Willie and Ernsty

On the Subway

Burlesque premiere in Germany

Nutella crepe

They sell these things in Berlin. They are delicious!

Making my nutella crepe

Raft alope

It was in the middle of potsdamer platz

Just getting started!

Dinner

Dinner on the top floor of KaDeWe

KaDeWe

A huge department store in Berlin.

Christmas market stand

Tons of colors

The new church

The new church

The new church

The new church

Gedaechtniskirche

What’s left of a very old church that was bombed in WWII

Travel bros

My first glass of Gluehwein!

A cup of really hot wine.

Kevin drinking Gluehwein