How to extend an ArrayAdapter?

by craiget » Fri, 10 Apr 2009 23:53:16 GMT


Sponsored Links
 Hello,

I'm running into difficulties overriding an ArrayAdapter to be
displayed in a ListActivity.

The ArrayAdapter is filled asynchronously.

What I'm trying to do is add a special "sentinel" object that sits at
the beginning of the list to show the progress of the computation.
Ultimately this may be a progress bar, but right now I am just using a
TextView as a placeholder.

The problem is that the sentinel seems to get inserted multiple times
into the list - not just once at the beginning. It *seems* to be
appearing once per "page" of list data (so if 8 list items fit on the
screen, my sentinel appears as the 1st, then 9th, then the 17th,
etc..), but that interpretation may not be accurate.

While maybe not how ArrayAdapters were intended to be extended, I
thought something like this should work by simply overriding getView()
to show my custom TextView for index 0:

public class CustomAdapter<E> extends ArrayAdapter<E> {

        private View sentinel;

        public CustomAdapter(Context context, int textViewResourceId, List<E>
objects) {
                super(context, textViewResourceId, objects);
                sentinel = new TextView(context);
                ((TextView)sentinel).setText("Still Loading..");
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
{
                if(position == 0)
                        return sentinel;
                return super.getView(position, convertView, parent);
        }

        public void finish() {
                //called when async list computation finishes
                remove(getItem(0));
        }

}

The adapter is created in the usual way:

...
ArrayList<String> data = new ArrayList<String>();
data.add('****'); //item 0, this should get replaced
adapter = new CustomAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1, data);
adapter.add("Sentinel");
setListAdapter(adapter);
...


My two guesses so far are: 1) When data is added asynchronously,
something goes wrong with redrawing the list view. 2) I'm not
correctly interpreting the "position" parameter of "getView()".

Any suggestions on why this doesn't work?

Thanks for reading folks.

 - C







--~--~---------~--~----~------------~-------~--~----~



How to extend an ArrayAdapter?

by Mark Murphy » Fri, 10 Apr 2009 23:58:11 GMT


 


Try addHeaderView().


You haven't indicated how you are adding stuff to the ArrayAdapter, and
your problem may lie in there.

However, I would use addHeaderView() for this circumstance, or have your
"sentinel" live outside the ListView. Remember: even though you are
using a ListActivity, you can still specify your own layout. You need
the magic android:id for the ListView (@android:id/list) in that case.

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

_The Busy Coder's Guide to Android Development_ Version 2.0 Available!

--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


How to extend an ArrayAdapter?

by craiget » Sat, 11 Apr 2009 00:28:35 GMT


 Fantastic. listView.addHeaderView() works like a charm!

It seems this usage was already thought of by folks more clever than
myself. I guess it helps to know the API =)

Thanks Mark.







--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Android 2.3.4 on Nexus One

I just got an OTA notification to update my Nexus One to version
2.3.4.

Where is a release notes for this update?  As a developer, it would be
nice to know what's has changed in the platform.

-- 

2. Chef Recipes Apps

Bagi rekan2 yg suka resep masakan,
Chef Recipes Apps Android Launching...

Android Market
https://market.android.com/details?id=mob.modis.android.chefrecipes

Slideme
http://slideme.org/en/application/chef-recipes

Coming soon amazon store

Bagi rekan2 dibandung, tgl 21 Mei ada seminar di UNIKOM Bandung, hub
panitia terdekat UNIKOM
Hub Sdri Margi: http://www.facebook.com/profile.php?id=1539044226

Thx
Rully Hasibuan
ERHA CORP.
http://mobdroid.wordpress.com/
twitter: @rullyhasibuan

-- 
"

3. How to get MD5 fingerprint for google maps in windows 7 ?

4. ROM Motorola XT800 2.3

5. I keep on getting 401 when sending msg to C2DM server

6. How to connect internet through android emulator application

7. Masalah Sinyal Motorola XT720 <= MOHON BANTUAN UNTUK ....