调用 notifyDataChanged() 方法时, ListView 未更新数据。
在 onCreate() 方法中,我初始化了没有数据的 ListView 。
ListView videoList = (ListView)findViewById(R.id.videos_list);
videoList.setOnItemClickListener(listener);
listAdapter = new PHVideosListAdapter(PHVideosActivity.this, videos);
videoList.setAdapter(listAdapter);
在此之后,我开始使用新的 VideosCategoryFetchTask().execute(); 获取视频列表;
在我调用的后执行方法中
@Override
protected void onPostExecute(Boolean success) {
if(success) {
listAdapter.notifyDataSetChanged();
} else {
//show dialog
}
}
但列表中没有显示任何内容。如果有人知道解决方案,请帮助...
private class VideosDetailsFetchTask extends AsyncTask<String, Void, Boolean> {
@Override
public void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(String... params) {
Boolean success = false;
try {
if (params.length >= 0) {
videos = (Videos)videoAPI.videosForCategoryId(params[0],new VideosParser());
success = true;
}
} catch (Exception e) {
// TODO: handle exception
}
return success;
}
@Override
protected void onPostExecute(Boolean success) {
if(success) {
progressBar.setVisibility(View.INVISIBLE);
onFinishVideoFetch();
} else {
//show dialog
}
}
}
这里使用两个异步类,第一个在第一个的 onPostExecute() 上调用。
private void onFinishVideoFetch() {
if(videos != null) {
listAdapter.notifyDataSetChanged();
}
}
我不会一个一个地获取视频..这里返回一个视频列表....
获取视频列表后,我想刷新列表。
@Override
protected void onProgressUpdate(Videos... values) {
videos = values[0];
//add published object to list which holds
listAdapter.notifyDataSetChanged();
}
我试过了,但运气不好,请帮忙..
这是使用的适配器类
公共(public)类 PHVideosListAdapter 扩展了 BaseAdapter{
private Videos videoTitles;
private LayoutInflater inflater;
public PHVideosListAdapter(Context context, Videos videoTitles) {
inflater = LayoutInflater.from(context);
this.videoTitles = videoTitles;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
if(videoTitles != null) {
return videoTitles.size();
}
else {
return 0;
}
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return videoTitles.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
VideoListViewHolder holder = null;
if (convertView == null) {
holder = new VideoListViewHolder();
convertView = inflater.inflate(R.layout.videos_list, null);
holder.videoTitle = (TextView) convertView.findViewById(R.id.video_title);
holder.videoImage = (ImageView) convertView.findViewById(R.id.video_image);
holder.videoDuration = (TextView) convertView.findViewById(R.id.video_duration);
convertView.setTag(holder);
} else {
holder = (VideoListViewHolder)convertView.getTag();
}
holder.videoImage.setImageResource(R.drawable.icon);
holder.videoDuration.setText("00:10");
holder.videoTitle.setText(videoTitles.get(position).getVideoTitle());
return convertView;
}
private class VideoListViewHolder {
ImageView videoImage;
TextView videoTitle;
TextView videoDuration;
}
最佳答案
当您第一次创建 PHVideosListAdapter
时,它持有对 Videos
列表的引用,我假设该列表是您的 Activity 的成员。在您的 doInBackground
方法中,对 videoAPI.videosForCategoryId
的调用正在更新您 Activity 中的 Videos
引用,但适配器仍持有原始引用传递给 PHVideosListAdapter
的构造函数。您需要在 onPostExecute
中重新创建 PHVideosListAdapter
或在 PHVideosListAdapter
中添加一个 set 方法来更改私有(private)变量 videoTitles
.
我在使用 Google 提供的 ArrayAdapter
时遇到了同样的问题。您只能在构造函数中设置底层 List
,因此您必须重新创建 ArrayAdapter 或创建您自己的类,以允许更改底层数据以使 notifyDataSetChanged
起作用。
关于android - notifyDataSetChanged 在 android 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4438785/