Hi, Instead of doing your complicated casts every time, you should simply keep a reference to the original adapter in your Activity; this is usually how we do it in the default Android apps :)
On Tue, Jan 13, 2009 at 5:29 AM, Chronos <[email protected]> wrote: > > Hi there ! > > Like many others, I had some trouble adding a View to the end of the > list; after reading some posts, I stumbled over the addHeaderView() / > addFooterView() methods of ListView. Those work perfectly for adding > Views to the beginning / end of the list (which do ALSO scroll). > Unfortunately this changes the behaviour of the ListView.getAdapter() > and is poorly documented. Instead of returning the associated adapter, > the ListView returns a HeaderViewListAdapter wrapped around the > original adapter. This (of course) resulted result in a > ClassCastException when I tried to modify the underlying adapter data. > Watch out for the strange casting/unwrapping construct: > ((ArrayAdapter<String>) ((HeaderViewListAdapter) listView.getAdapter > ()).getWrappedAdapter()).notifyDataSetChanged(). I attach my test code > for those who want to play around with it. > > For those who are looking for static header / footer Views, try > android:layout_height="0dip" and android:layout_weight="1" as Romain > Guy already pointed out several times (thanks a lot ;). > > Happy coding :) > > AndroidManifest.xml: > > <?xml version="1.0" encoding="utf-8"?> > <manifest xmlns:android="http://schemas.android.com/apk/res/android" > package="debug.d3bugg0r" > android:versionCode="1" > android:versionName="1.0.0"> > <application android:icon="@drawable/icon" android:label="@string/ > app_name"> > <activity android:name=".D3bugg0r" > android:label="@string/app_name"> > <intent-filter> > <action android:name="android.intent.action.MAIN" /> > <category > android:name="android.intent.category.LAUNCHER" /> > </intent-filter> > </activity> > </application> > </manifest> > > > > res/layout/main.xml: > > <?xml version="1.0" encoding="utf-8"?> > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/ > android" > android:orientation="vertical" > android:layout_width="fill_parent" > android:layout_height="fill_parent"> > <ListView > android:id="@+id/listView" > android:layout_width="fill_parent" > android:layout_height="wrap_content"/> > </LinearLayout> > > > > src/debug/d3bugg0r/D3bugg0r.java: > > package debug.d3bugg0r; > > import java.util.ArrayList; > import java.util.List; > > import android.app.Activity; > import android.os.Bundle; > import android.view.View; > import android.widget.ArrayAdapter; > import android.widget.Button; > import android.widget.HeaderViewListAdapter; > import android.widget.ListView; > > public class D3bugg0r extends Activity implements View.OnClickListener > { > private ListView listView; > private Button addButton; > private Button removeButton; > private List<String> strings; > > @Override > public void onCreate(Bundle savedInstanceState) > { > super.onCreate(savedInstanceState); > setContentView(R.layout.main); > listView = (ListView) findViewById(R.id.listView); > addButton = new Button(this); > addButton.setText("ADD"); > addButton.setOnClickListener(this); > removeButton = new Button(this); > removeButton.setText("REMOVE"); > removeButton.setOnClickListener(this); > listView.addFooterView(addButton); > listView.addFooterView(removeButton); > strings = new ArrayList<String>(); > strings.add("First"); > strings.add("Second"); > strings.add("Third"); > listView.setAdapter(new ArrayAdapter<String>(this, > android.R.layout.simple_list_item_1, strings)); > } > > @SuppressWarnings("unchecked") > @Override > public void onClick(View view) > { > if(view == addButton) > { > strings.add("another one"); > ((ArrayAdapter<String>) ((HeaderViewListAdapter) > listView.getAdapter > ()).getWrappedAdapter()).notifyDataSetChanged(); > } > else if(view == removeButton) > { > if(strings.size() > 0) > { > strings.remove(strings.size() - 1); > ((ArrayAdapter<String>) > ((HeaderViewListAdapter) > listView.getAdapter()).getWrappedAdapter()).notifyDataSetChanged(); > } > } > } > } > > > > -- Romain Guy Android framework engineer [email protected] Note: please don't send private questions to me, as I don't have time to provide private support. All such questions should be posted on public forums, where I and others can see and answer them --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---

