我正在审核Android上的代码但不了解在扩展notifyDataSetChanged()
的类的对象上调用BaseAdapter
时会发生什么。
在代码中,调用了channelListAdapter.notifyDataSetChanged()
。
以下是ChannelListAdapter
类的代码:
public class ChannelListAdapter extends BaseAdapter {
Field[] fields;
Context context;
List<Channels> channelList = null;
String dateString, likeData, ab, imageData;
Channels item;
DBVideos dbVideos;
VideoListAdapter videoListAdapter;
MostRecentFragment parentFragment;
public boolean loadingMoreChannels;
public boolean noMoreChannelFound;
Map<String, Boolean> map = new HashMap<String, Boolean>();
Map<String, Integer> totalLikeCount = new HashMap<String, Integer>();
public Map<String, Boolean> timerMap = new HashMap<String, Boolean>();
Map<String, List<Videos>> mapVideos = new HashMap<String, List<Videos>>();
public ChannelListAdapter(Context context) {
this.context = context;
}
public Map<String, List<Videos>> getMapVideos() {
return mapVideos;
}
public void setMapVideos(Map<String, List<Videos>> mapVideos) {
this.mapVideos = mapVideos;
}
public ChannelListAdapter(Context context, List<Channels> channelList) {
this.context = context;
// listener = new NewsFeedListener(context, map, totalLikeCount);
// listener.setListAdapter(this);
this.channelList = channelList;
}
public ChannelListAdapter(Context context, List<Channels> channelList, MostRecentFragment parentFragment) {
this.context = context;
this.parentFragment = parentFragment;
this.channelList = channelList;
}
public String getLastFeedModificationTime() {
Channels lastNews = channelList.get(channelList.size() - 1);
return lastNews.getLastModifyDate();
}
public void setNewsList(List<Channels> channelList) {
this.channelList = channelList;
}
@Override
public int getCount() {
return channelList.size();
}
@Override
public Object getItem(int position) {
return channelList.get(position);
}
@Override
public long getItemId(int position) {
return channelList.indexOf(getItem(position));
}
public static class ViewHolder {
public TextView channelTitle;
public ImageView imgProcess;
ListView videoListView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = (View) mInflater.inflate(R.layout.channel_list_item, null);
holder.channelTitle = (TextView) convertView.findViewById(R.id.tv_channel_name);
holder.imgProcess = (ImageView) convertView.findViewById(R.id.img_process);
holder.videoListView = (ListView) convertView.findViewById(R.id.list_videos);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
item = (Channels) getItem(position);
final ViewHolder finalHolder = holder;
holder.channelTitle.setText(item.getTitle());
if (mapVideos.size() > 0 && mapVideos.containsKey(item.getId())) {
} else {
Glide.with(context).load("http://4.bp.blogspot.com/-rSoHhekcu9A/T6PQ6d8mKSI/AAAAAAAAASg/hStx9Mg18fc/s1600/22.gif").asGif().placeholder(R.drawable.img_loading).crossFade()
.into(holder.imgProcess);
String url = null;
url = context.getString(R.string.url_6e_channel_context)+CommonUtils.getEncodedUrl(context, context.getString(R.string.url_to_retive_all_video_selected_channel_first)+item.getId())+"+ContentType:Indigo+Video'&rowlimit=3&"+CommonUtils.getEncodedUrl(context,context.getString(R.string.url_to_retive_all_video_selected_channel_sec));
System.out.println("url ="+url);
ServiceCallAsync sca = new ServiceCallAsync(context, null,"Get", null, url, new AsyncResponse() {
@Override
public void processFinish(Object output) {
ServiceResponse serviceResponse = (ServiceResponse) output;
if(serviceResponse.getCode()==200){
parseDataAndStoreinDb(serviceResponse.getData(),finalHolder);
}else{
}
}
});
sca.execute();
}
if (position == channelList.size() - 1) {
if (parentFragment != null && !loadingMoreChannels && !noMoreChannelFound) {
loadingMoreChannels = true;
parentFragment.loadMore();
}
}
return convertView;
}
protected void parseDataAndStoreinDb(String data, ViewHolder finalHolder) {
List<Videos> videosList = new ArrayList<>();
try {
JSONObject mainObj = new JSONObject(data);
JSONObject dObj = mainObj.getJSONObject("d");
JSONObject queryObj = dObj.getJSONObject("query");
JSONObject primaryQueryObj = queryObj.getJSONObject("PrimaryQueryResult");
JSONObject releventObj = primaryQueryObj.getJSONObject("RelevantResults");
JSONObject tableObj = releventObj.getJSONObject("Table");
JSONObject rowsObj = tableObj.getJSONObject("Rows");
JSONArray resultArray = rowsObj.getJSONArray("results");
for(int i = 0; i<resultArray.length();i++){
Map<String ,String> videoFieldsMap=new HashMap<String, String>();
JSONObject resultObj= resultArray.getJSONObject(i);
JSONObject cellesObj= resultObj.getJSONObject("Cells");
JSONArray resultInnerArray = cellesObj.getJSONArray("results");
for(int j = 0; j<resultInnerArray.length();j++){
JSONObject obj= resultInnerArray.getJSONObject(j);
//System.out.println("obj ="+obj.getString("Key"));
videoFieldsMap.put(obj.getString("Key"), obj.getString("Value"));
}
videoFieldsMap.put("ListId", item.getId());
Videos video = convertMapToVideoModel(videoFieldsMap);
System.out.println("video convertedMap ="+video);
videosList.add(video);
}
if(dbVideos == null ){
dbVideos = new DBVideos(context);
}
dbVideos.insertVideos(videosList);
// mapVideos.put(item.getId(), videosList);
setVideoListAdapter(videosList,finalHolder);
} catch (JSONException e) {
e.printStackTrace();
}
}
private void setVideoListAdapter(List<Videos> list, ViewHolder finalHolder) {
if(videoListAdapter == null){
videoListAdapter = new VideoListAdapter(context, list,parentFragment);
}
videoListAdapter.setVideoList(list);
System.out.println("list ="+list.size());
System.out.println("list ="+list.get(1));
finalHolder.videoListView.setAdapter(videoListAdapter);
finalHolder.videoListView.setVisibility(View.VISIBLE);
finalHolder.imgProcess.setVisibility(View.GONE);
ListHeightUtils.setListViewHeightBasedOnChildren(finalHolder.videoListView);
}
private Videos convertMapToVideoModel(Map<String, String> videoFieldsMap){
Class clazz=Videos.class;
Object object = null;
System.out.println("videoFieldsMap ="+videoFieldsMap);
try{
object=clazz.newInstance();
for(Field field:clazz.getDeclaredFields()){
field.setAccessible(true);
System.out.println("field =="+videoFieldsMap.get(field));
field.set(object, videoFieldsMap.get(field.getName()));
}
System.out.println("object ="+object.toString());
}catch(Exception e){
e.printStackTrace();
}
return (Videos)object;
}
}
答案 0 :(得分:1)
适配器定义了在视图中显示项目列表的规则 - 它通常位于ListView或RecyclerView的上下文中(但实际上可以是多种内容)。视图不知道基础数据集何时发生更改,因此在初始绘制之后,需要告知其数据何时不再是最新数据。
假设您有以下列表:
[A,B,C]
在某些时候,用户点击API并更新列表:
[A,B,C, D , E , F ]
显示此列表的ListView仍将仅显示:
[A,B,C]
当您在附加到ListView的适配器上调用notifyDataSetChanged()时,它将更新ListView以显示完整的新数据集:
[A,B,C,D,E,F]
根据您使用的适配器类型,也可以通知整个数据集已更改,或者执行更多特定的更新调用,例如RecyclerView的notifyItemChanged
,notifyItemRemoved
,{ {1}}等。
答案 1 :(得分:0)
来自docs notifyDataSetChanged():
通知附加的观察者基础数据已被更改,反映数据集的任何视图都应自行刷新。
您的应用程序中可能需要更新ListView
,以便绑定到listview
的数据集发生变化时add
,update
或delete
。
要实现此功能,Android会为ArrayAdapter
或BaseAdapter
调用notifyDataSetChanged(),以更新listview
。