gpt4 book ai didi

java - 在扩展 Base Adapter 的类的对象上调用 notifyDataSetChanged() 时会发生什么

转载 作者:行者123 更新时间:2023-11-30 01:24:28 27 4
gpt4 key购买 nike

我正在审查 Android 上的代码,但不明白在扩展 BaseAdapter 的类的对象上调用 notifyDataSetChanged() 时发生了什么。在代码中,调用了 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;
}

}

最佳答案

Adapter 定义了在 View 中显示项目列表的规则 - 它通常在 ListView 或 RecyclerView 的上下文中(但实际上可以是很多东西)。 View 不知道底层数据集何时更改,因此在其初始绘制后,需要告知其数据何时不再是最新的。

假设您有以下列表:

[A, B, C]

在某个时候,用户点击 API 并更新列表:

[A, B, C, D, E, F]

显示这个列表的ListView仍然只会显示:

[A, B, C]

当您在附加到 ListView 的适配器上调用 notifyDataSetChanged() 时,它将更新 ListView 以显示完整的新数据集:

[A, B, C, D, E, F]

根据您使用的适配器类型,也可以通知整个数据集已更改,或者执行更具体的更新调用,例如 RecyclerView 的 notifyItemChangednotifyItemRemoved , notifyItemMoved

关于java - 在扩展 Base Adapter 的类的对象上调用 notifyDataSetChanged() 时会发生什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36601756/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com