notifyDataSetChanged() will only work on the UI thread.  In order to
do this, create a simple Runnable:

        // adapter is a reference to your BaseAdapter or ListAdapter
        private Runnable updateAdapter = new Runnable() {

                @Override
                public void run() {
                        // TODO Auto-generated method stub
                        adapter.notifyDataSetChanged();
                }

        };

       //Assumes this called by a thread you created separately, and
as a method of the Activity that needs to refresh
       private void handleAsyncResponse() {
            //Do some stuff...
            runOnUiThread(updateAdapter);
       }

I am actually thinking about creating some sort of utility class that
handles this mess transparently.

The reason biosopher's example works is that the event handlers are
always called by the UI thread.

On Oct 30, 12:43 pm, Emery <[EMAIL PROTECTED]> wrote:
> You'redoingitonItemClick,soit'sprobablyinvalidatedforredraw
> anyway. In my case the items are coming in on a separate thread. If I
> was to click an itemitwould update then. But of course that's a very
> weird interaction to touch the screen and suddenly all of the updates
> show up at once.
>
> On Oct 30, 9:28 am, Biosopher <[EMAIL PROTECTED]> wrote:
>
> > I used notifyDataSetChanged() anditworks fine for me.  Here's my
> > complete code with the notifyDataSetChanged() at the bottom in the
> > selection listener:
>
> > package com.pocketjourney.location.map;
>
> > import java.util.List;
>
> > import android.content.Context;
> > import android.view.LayoutInflater;
> > import android.view.View;
> > import android.view.ViewGroup;
> > import android.widget.AdapterView;
> > import android.widget.BaseAdapter;
> > import android.widget.ImageView;
> > import android.widget.ListView;
> > import android.widget.TextView;
> > import android.widget.AdapterView.OnItemClickListener;
>
> > import com.pocketjourney.R;
> > import com.pocketjourney.location.DestinationModel;
>
> > public class DestinationTypesListAdapter  extends BaseAdapter
> > implements OnItemClickListener
>
> > {
> >         private List<String>      allTypes, activeTypes;
>
> >         private ListView destinationTypeList;
>
> >         public DestinationTypesListAdapter(ListView destinationTypeList)
> >         {
> >                 this.destinationTypeList = destinationTypeList;
>
> >                 this.allTypes = DestinationModel.getAllDestinationTypes();
> >                 this.activeTypes = 
> > DestinationModel.getActiveDestinationTypes();
>
> >                 destinationTypeList.setOnItemClickListener(this);
> >         }
>
> >         public View getView(int position, View convertView, ViewGroup
> > parent)
> >         {
> >                 View destinationTypeLabel = null;
> >                 if ( convertView == null )
> >                 {
> >                         LayoutInflater inflater =
> > (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
> >                         destinationTypeLabel =
> > inflater.inflate(R.layout.list_item_destination_type, null);
> >                 } else {
> >                         destinationTypeLabel = convertView;
> >                 }
>
> >                 String destinationType = (String) getItem(position);
> >                 TextView destinationTypeText = (TextView)
> > destinationTypeLabel.findViewById(R.id.destination_type);
> >                 destinationTypeText.setText(destinationType);
>
> >                 ImageView typeSelectedImage= (ImageView)
> > destinationTypeLabel.findViewById(R.id.destination_type_selected);
> >                 if (activeTypes.contains(destinationType)) {
>
> > typeSelectedImage.setImageResource(R.drawable.list_selection_checkmark);
> >                 } else {
> >                         
> > typeSelectedImage.setImageResource(R.drawable.empty);
> >                 }
>
> >                 return destinationTypeLabel;
> >         }
>
> >         public int getCount()
> >         {
> >                 return allTypes.size();
> >         }
>
> >         @Override
> >         public Object getItem(int position)
> >         {
> >                 return allTypes.get(position);
> >         }
>
> >         @Override
> >         public long getItemId(int position) {
> >                 // We can simply use the array index as a unique id.
> >                 return position;
> >         }
>
> >         /**
> >          *  Implementation of OnItemClickListener
> >          */
> >         @Override
> >         public voidonItemClick(AdapterView adapterView, View view, int
> > position, long id)
> >         {
> >                 String destinationType = (String) getItem(position);
> >                 if (activeTypes.contains(destinationType)) {
> >                         activeTypes.remove(destinationType);
> >                 } else {
> >                         activeTypes.add(destinationType);
> >                 }
> >                 DestinationModel.setActiveDestinationTypes(activeTypes);
> >                 notifyDataSetChanged();
> >         }
>
> > }
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to