stumble, stumble – make my listview work dammit!!!

I have a spinner that I need to use and fill from several different components – at least two Activity classes and one Dialog. Looking for a way to reuse the same code and layouts; not clear just yet. But along the way I came across this forum post which I’ll have to look at next time I’m trying to build a custom view.

Also I have a custom dialog that only enables the “OK” button if a text editor has text in it, and wanted to generalize that. The problem is with how much stuff you have to specify to wire it all together. I think what I really want is an AlertDialog with a text field and some extra magic to enable/disable the positive button, but to get all that working I’d pretty much need to subclass AlertDialog.Builder too, and not sure if that’s worth the pain. But perhaps worth a try. You’d think this would be a standard feature, though… a dialog that validates input before enabling the buttons to be pushed.

Next problem: I have a ListView and I want to have a spinner at the top and a text entry box at the bottom. Putting this inside a ScrollView does not work, I get this:

How do I get the ListView to stay fully expanded? I found several forum posts warning against using a ListView within a ScrollView, so that’s out. The main commentator on this seems to be Romain Guy, who’s obviously very knowledgeable, but he’s also really lousy at adequately answering questions. His blog entries on Android are pretty sparse. I still wanna read them but man is it frustrating reading his half-answers in forums.

Looking for ListView attrs… Well, the ListView reference docs say that you can add header and footer elements; any way to do that in XML? Does not look that way. I’ll try it in code first. When I first used ListView, I naively thought that you should place the “empty” item inside the ListView element in the XML. Certainly makes more sense than putting it after, don’t you think? I mean, at least conceptually it’s part of the view; but the designers thought otherwise. Actually, at this point it looks like anything you place after the ListView element is used for the empty view. Anyway, I put a header element before and footer element after in the file, and added them to the ListView in code like so:

        getListView().addHeaderView(findViewById(R.id.il_list_spinner));
        getListView().addFooterView(findViewById(R.id.il_new_item));

This causes a lovely runtime exception, which basically tells you nothing about what went wrong:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{net.sosiouxme.WhenDidI/net.sosiouxme.WhenDidI.activity.ItemList}: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
[…]
 Caused by: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
     at android.widget.ListView.clearRecycledState(ListView.java:489)
     at android.widget.ListView.resetList(ListView.java:475)
     at android.widget.ListView.setAdapter(ListView.java:417)
[…]

Pretty helpful eh? Yeah. Might as well just say “dude… something’s wrong with your layout.” I’m guessing it doesn’t like me reusing view elements of the current context as elements of the ListView. Or maybe it’s related to the “poorly documented” bit mentioned in this thread (this would be more helpful with a pointer to the back story). And now I can’t figure out how to debug down into platform code – is there some way to attach source code so the debugger can look at it? Can’t seem to find anything on that in a search either. Good grief, this stuff makes me feel like an idiot!

Here’s some articles on customizing ListView to read later. Not germaine to my current purpose AFAICS, unfortunately.

As an aside – the Eclipse tool for building XML layout resources, while helpful, is really annoying. Chief among the annoyances is when you’re trying to scroll through the list of properties for a tag – each attribute has a little blurb about what it’s for, which conveniently pops up to cover what you’re trying to look at every time you pause! And if you’re really lucky, it pops up right under the mouse, keeping you from scrolling. You know, what I need is not automatic popups – I’d rather have a mouse button specifically to give me info about what I’m looking at. The middle mouse button would probably be perfect since I don’t think Eclipse does anything else with it. Looking for preferences I can set to disable or control the hover, but even with all hovers disabled this still pops up. Wonder if it would do me any good to try to fix ADT?

And why is it that adb keeps losing track of its emulators? Kill the server and restart it, and it finds the emulator again… only to lose it again quickly. Bah!

Advertisements

2 Responses

  1. Hi Luke,
    I face a similar problem. Same error message while adding a header to my ListView.
    Did you finally manage it?
    Thanks, Jana

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: