HeaderView in ListView causing ClassCastException

by bobetko » Sat, 11 Dec 2010 00:37:30 GMT


Sponsored Links
 I have added a view to the header of my ListView by following way:

        ListView lv = (ListView)getListView();
        View TopSearch =  (View) View.inflate(this, R.layout.search,
null);
        lv.addHeaderView(TopSearch, null, false);

And everything is fine until I make changes to data adapter and then
try to execute:

       adapter.notifyDataSetChanged();

This always crashes my application giving me following error:

"java.lang.ClassCastException: android.widget.HeaderViewListAdapter"

If I don't add header view to my list view, then everything works fine
with no errors
Any suggestions?

Thanks.

-- 



Re: HeaderView in ListView causing ClassCastException

by TreKing » Sat, 11 Dec 2010 01:18:43 GMT


 




Post the full stack trace?

-------------------------------------------------------------------------------------------------
TreKing < http://sites.google.com/site/rezmobileapps/treking> ; - Chicago
transit tracking app for Android-powered devices

-- 


Sponsored Links


Re: HeaderView in ListView causing ClassCastException

by bobetko » Sat, 11 Dec 2010 01:37:51 GMT


 Here it is:

12-10 11:35:28.111: ERROR/AndroidRuntime(25364): FATAL EXCEPTION: main
12-10 11:35:28.111: ERROR/AndroidRuntime(25364):
java.lang.ClassCastException: android.widget.HeaderViewListAdapter
12-10 11:35:28.111: ERROR/AndroidRuntime(25364):     at
com.sanantonio.cvb.PropertyList$1.onTextChanged(PropertyList.java:130)
12-10 11:35:28.111: ERROR/AndroidRuntime(25364):     at
android.widget.TextView.sendOnTextChanged(TextView.java:6131)
12-10 11:35:28.111: ERROR/AndroidRuntime(25364):     at
android.widget.TextView.handleTextChanged(TextView.java:6172)
12-10 11:35:28.111: ERROR/AndroidRuntime(25364):     at
android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:
6316)
12-10 11:35:28.111: ERROR/AndroidRuntime(25364):     at
android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:
889)
12-10 11:35:28.111: ERROR/AndroidRuntime(25364):     at
android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:
502)
12-10 11:35:28.111: ERROR/AndroidRuntime(25364):     at
android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:
409)
12-10 11:35:28.111: ERROR/AndroidRuntime(25364):     at
android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:
28)
12-10 11:35:28.111: ERROR/AndroidRuntime(25364):     at
android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:
583)
12-10 11:35:28.111: ERROR/AndroidRuntime(25364):     at
android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:
384)
12-10 11:35:28.111: ERROR/AndroidRuntime(25364):     at
com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:
292)
12-10 11:35:28.111: ERROR/AndroidRuntime(25364):     at
com.android.internal.view.IInputConnectionWrapper
$MyHandler.handleMessage(IInputConnectionWrapper.java:73)
12-10 11:35:28.111: ERROR/AndroidRuntime(25364):     at
android.os.Handler.dispatchMessage(Handler.java:99)
12-10 11:35:28.111: ERROR/AndroidRuntime(25364):     at
android.os.Looper.loop(Looper.java:123)
12-10 11:35:28.111: ERROR/AndroidRuntime(25364):     at
android.app.ActivityThread.main(ActivityThread.java:4627)
12-10 11:35:28.111: ERROR/AndroidRuntime(25364):     at
java.lang.reflect.Method.invokeNative(Native Method)
12-10 11:35:28.111: ERROR/AndroidRuntime(25364):     at
java.lang.reflect.Method.invoke(Method.java:521)
12-10 11:35:28.111: ERROR/AndroidRuntime(25364):     at
com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:858)
12-10 11:35:28.111: ERROR/AndroidRuntime(25364):     at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-10 11:35:28.111: ERROR/AndroidRuntime(25364):     at
dalvik.system.NativeStart.main(Native Method)

-- 



Re: HeaderView in ListView causing ClassCastException

by TreKing » Sat, 11 Dec 2010 01:42:40 GMT


 




When do you set your adapter?

-------------------------------------------------------------------------------------------------
TreKing < http://sites.google.com/site/rezmobileapps/treking> ; - Chicago
transit tracking app for Android-powered devices

-- 



Re: HeaderView in ListView causing ClassCastException

by bobetko » Sat, 11 Dec 2010 02:01:21 GMT


 I set it in activity's OnCreate method with

        myAdapter = new myBaseAdapter(this);
        lv.setAdapter(myAdapter);

and myAdapter is declared as
private myBaseAdapter myAdapter;
and visible in all activity methods.

Then (maybe important)
I have text watcher, which onTextChanged execute:

                    final BaseAdapter adapter = (BaseAdapter)
lv.getAdapter();
                    ((Filterable) adapter).getFilter().filter(s);          //
s is a search string

myBaseAdapter implements Filterable which execute
notifyDataSetChanged() when new filter is applied.

As I said before, the code doesn't produce any error if I comment 2
lines where I add header view.

Thanks,






-- 



Re: Re: HeaderView in ListView causing ClassCastException

by TreKing » Sat, 11 Dec 2010 02:15:26 GMT


 



And where do you call setHeaderView in relation to this?

-------------------------------------------------------------------------------------------------
TreKing < http://sites.google.com/site/rezmobileapps/treking> ; - Chicago
transit tracking app for Android-powered devices

-- 



Re: HeaderView in ListView causing ClassCastException

by John » Sat, 11 Dec 2010 02:15:39 GMT


 This looks like the problem here. Is that your code? What is line 130?




-- 



Re: HeaderView in ListView causing ClassCastException

by bobetko » Sat, 11 Dec 2010 02:34:32 GMT


 line 130 is this:

((Filterable) adapter).getFilter().filter(s);

-- 



Re: HeaderView in ListView causing ClassCastException

by bobetko » Sat, 11 Dec 2010 02:36:23 GMT


 I call setHeaderView in onCreate, before I set myAdapter.

-- 



Re: Re: HeaderView in ListView causing ClassCastException

by TreKing » Sat, 11 Dec 2010 02:39:29 GMT


 




OK, that eliminates that.

((Filterable) adapter).getFilter().filter(s);


Does your adapter extend Filterable?

-------------------------------------------------------------------------------------------------
TreKing < http://sites.google.com/site/rezmobileapps/treking> ; - Chicago
transit tracking app for Android-powered devices

-- 



Re: Re: HeaderView in ListView causing ClassCastException

by Kostya Vasilyev » Sat, 11 Dec 2010 02:52:10 GMT


 Bob,

The error is caused by addHeaderView wrapping your ListView adapter.

public void addHeaderView (View v) Since: API Level 1 Add a fixed view to appear at the top of the list. If addHeaderView is called more than once, the views will appear in the order they were added. Views added using this call can take focus if they want. NOTE: Call this before calling setAdapter. *This is so ListView can wrap the supplied cursor with one that will also account for header and footer views.*
So if you call getAdapter right after calling setAdapter (and in case there was a prior addHeaderView), you will not get back the same object, you will get a HeaderViewListAdapter. It's actually in your logcat. Your adapter implements Filterable, but android.widget.HeaderViewListAdapter does not. Hence the class cast exception. I can think of two ways to fix this: - Do not use addHeaderView, instead add the header layout into the ListView's parent, positioned right above. - Might also work: keep a reference to your original adapter object, the one that implements Filterable, and use that object reference for casting to Filterable. -- Kostya 10.12.2010 21:39, TreKing ?????:
On Fri, Dec 10, 2010 at 12:36 PM, bobetko <bobe...@gmail.com I call setHeaderView in onCreate, before I set myAdapter. OK, that eliminates that. ((Filterable) adapter).getFilter().filter(s); Does your adapter extend Filterable? ------------------------------------------------------------------------------------------------- TreKing < http://sites.google.com/site/rezmobileapps/treking> ; - Chicago transit tracking app for Android-powered devices --
-- Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com --



Re: Re: HeaderView in ListView causing ClassCastException

by Mark Murphy » Sat, 11 Dec 2010 03:08:14 GMT


 You can also call getWrappedAdapter() on the HeaderViewListAdapter to
get the underlying adapter, casting it to Filterable or whatever.







-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://github.com/commonsguy 
 http://commonsware.com/blog  |  http://twitter.com/commonsguy 

Android App Developer Books:  http://commonsware.com/books 

-- 



Re: HeaderView in ListView causing ClassCastException

by bobetko » Sat, 11 Dec 2010 03:10:43 GMT


 Yes. myAdapter implements Fliterable

-- 



Re: HeaderView in ListView causing ClassCastException

by bobetko » Sat, 11 Dec 2010 03:13:07 GMT


 Thank you very much Kostya.

-- 



Re: HeaderView in ListView causing ClassCastException

by blindfold » Sat, 11 Dec 2010 04:09:06 GMT


  http://stackoverflow.com/questions/4393775/android-classcastexception-when-adding-a-header-view-to-expandablelistview 
helped me out with addHeaderView() and the infamous
ClassCastException.

Basically you do TopSearch.setLayoutParams(new
ListView.LayoutParams(ListView.LayoutParams.FILL_PARENT,
ListView.LayoutParams.WRAP_CONTENT)); right before
lv.addHeaderView(TopSearch);

Regards




-- 



Other Threads

1. Is it possible to have a single ui element instance across the application?

It doesnt seem possible, all code I've looked at from Google applications 
repeats the common portions of activity display in all layout files.

ll...@meridja.com






>

2. R.java file of my project is deleted.......how to get it back

R.java is generated automagically, you don't need to create or update it, the 
sdk will do that for you.

If you're using eclipse just refresh your imports ctrl+shift+o to reimport the 
correct one, the one with your package appended to it.

ll...@meridja.com




>

3. How to use internet in htc mobile if net is in my pc

4. Request to publich app in Korea

5. R.java file of my project is deleted.......how to get it back

6. Jar in Jar

7. Is it possible to have a single ui element instance across the application?