Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[done]notifyDataSetChanged() does not update ListActivity automatically

Tags:

android

I've got some troubles with notifyDataSetChanged() of a BaseAdapter. This method is called in refreshItems() and shall update the BaseAdapter of my ListActivity. On calling notifyDataSetChanged() nothing happens until I scroll down the ListView for example with the arrow keys. Somehow the modified getView() method also is not called. Maybe you can give me a hint - thanks! :)

public class WinampControlClientPlaylist extends ListActivity {

static WinampControlClientPlaylist activity = null;

static EfficientAdapter adapter = null;


static class EfficientAdapter extends BaseAdapter {


    public EfficientAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

    private LayoutInflater mInflater;

    @Override
    public int getCount() {
        return Settings.playlistlength;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;


        if (convertView == null) 
        {
            holder = new ViewHolder();

            convertView = mInflater.inflate(R.layout.listview, null);

            holder.text = (TextView) convertView.findViewById(R.string.playlist_title);
            holder.image = (ImageView) convertView.findViewById(R.string.playlist_play);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        holder.text.setText(Settings.playlist[position]);

        if (position == Settings.playlistPosition)
        {
            holder.text.setTypeface(null, Typeface.ITALIC);
            holder.image.setVisibility(0);
        }
        else
        {
            holder.text.setTypeface(null, Typeface.NORMAL);
            holder.image.setVisibility(4);
        }


        return convertView;
    }


    static class ViewHolder {
        TextView text;
        ImageView image;
    }

    @Override
    public Object getItem(int position) {
        return Settings.playlist[position];
    }
}




void initialize()
{
    adapter = new EfficientAdapter(this);
    setListAdapter(adapter);

    //registerForContextMenu(getListView());
}


@Override
public void onResume()
{
    super.onResume();

    // REFRESH PLAYLIST
    if (getListAdapter() == null && Settings.playlist != null)
        initialize();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.playlist);

    activity = this;
}

static void refreshItems()
{
    try {
        adapter.notifyDataSetChanged();
    } catch (Exception e) {}

}

}

like image 589
martin Avatar asked Mar 03 '11 20:03

martin


2 Answers

I had the same problem (ListView updates only when i scroll it, even notifyDataSetChanged didn't help). i solve it this way: just try to do your "view modifications" in thread which creates your user interface i.e.

activity.runOnUiThread(new Runnable() {         
        public void run() {
              //do your modifications here

              // for example    
              adapter.add(new Object());
              adapter.notifyDataSetChanged()  
        }
});
like image 80
ashakirov Avatar answered Sep 27 '22 19:09

ashakirov


Try calling invalidate() on your ListView.

like image 44
whlk Avatar answered Sep 27 '22 20:09

whlk