我正在尝试显示从网址中提取的项目列表,但我只想一次获取其中的20个…所以我已经实现了一个OnScrollListener来获取用户在最后一项上的项目列表显示.
提取的项目,但我唯一的问题是列表视图没有更新:这是我的代码到目前为止:
@Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { parentView = inflater.inflate(R.layout.activity_event_speakers_alphabetically,container,false); listView = (ListView) parentView .findViewById(R.id.speakersAlphabeticallyActivityList); nameOrderedMembers.addDataBatch(communityMembers); nameOrderedAdapter = DelegateViewStateAdapterFactory .makeUserListAdapter(getActivity(),nameOrderedMembers.getSortedData(),DelegateViewStateAdapterFactory.OrderType.NAME); listView.setAdapter(nameOrderedAdapter); AQuery aQuery = new AQuery(listView); aQuery.id(R.id.speakersAlphabeticallyActivityList).scrolled(new EndlessScrollListener()); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> arg0,View arg1,int arg2,long arg3) { if (nameOrderedAdapter.getItem(arg2) instanceof User) { User u = (User) nameOrderedAdapter.getItem(arg2); Intent i = new Intent(getActivity(),UserProfileFragmentActivity.class); startActivity(i); } } }); return parentView; }
现在 – 在onPostExecute方法的AsynkTask中 – 我像这样通知适配器:
communityMembers.addAll(newMembers); getActivity().runOnUiThread(new Runnable() { @Override public void run() { nameOrderedAdapter.addAll(communityMembers); nameOrderedAdapter.notifyDataSetChanged(); } });
但是列表视图不会刷新 – 只有当我返回并重新打开活动时 – 才会显示新数据.
解决方法
在notifyDataSetChanged()之后调用listView.invalidateViews()