[完成] notifyDataSetChanged()不会自动更新ListActivity自动更新、notifyDataSetChanged、ListActivity

2023-09-13 23:52:48 作者:charm丨灬梦之队

我有一些麻烦了BaseAdapter的notifyDataSetChanged()。这种方法被称为在refreshItems()并更新我的ListActivity的BaseAdapter。在调用notifyDataSetChanged()没有任何反应,直到我向下滚动的ListView例如使用箭头键。不知怎的,修改后的getView()方法还没有被调用。也许你可以给我一个提示 - 谢谢! :)

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) {}

}

}

推荐答案

我有同样的问题(ListView控件更新,只有当我把它的滚动,甚至notifyDataSetChanged没有帮助)。我解决这个问题是这样的:只是尝试做你的观点的修改,在线程,创建用户界面,即

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()  
        }
});