我正在使用ExpandableListView和自定义适配器,并且正在调用一个方法来重置我的适配器中的对象列表。我从异步任务传递该列表,该任务从API检索数据。当我检查适配器方法中新传递的列表时,它显示列表大小为零。在传递之前的asynctask它不是。 发生了什么? 在异步中:
protected void onPostExecute(String result) {
try {
JsonObject jo = new JsonParser().parse(result).getAsJsonObject();
JsonArray jsonArray = jo.getAsJsonArray("data");
Log.e("array:", jsonArray.toString());
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd hh:mm:ss.S")
.create();
Post[] posts = gson.fromJson(jsonArray, Post[].class);
offset = offset + posts.length;
if(posts.length<limit)
completed = true;
for(Post p : posts) {
postList.add(p);
Log.e("Post:", p.toString());
}
expandableListAdapter.setPosts(postList);
expandableListAdapter.notifyDataSetChanged();
loading = false;
}
catch (Exception e) {
Log.e("In PostExecute of Home", "exception: ", e);
}
适配器:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<Post> posts;
public void setPosts(List<Post> p ){
this.posts.clear();
this.posts.addAll(p);
Log.e(":size",String.valueOf(p.size()));
}
public ExpandableListAdapter(Context context, List<Post> posts) {
this.context = context;
this.posts = posts;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
super.registerDataSetObserver(observer);
}
@Override
public Comment getChild(int groupPosition, int childPosititon) {
Post post = posts.get(groupPosition);
if (post.getCommentList().size() > childPosititon) {
return post.getCommentList().get(childPosititon);
}
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
Comment comment = getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.activity_home_comment, null);
}
TextView commentView = (TextView) convertView.findViewById(R.id.commentV);
commentView.setText(comment.toString());
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this.posts.get(groupPosition).getCommentList().size();
}
@Override
public Post getGroup(int groupPosition) {
return this.posts.get(groupPosition);
}
@Override
public int getGroupCount() {
return this.posts.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Post post = this.posts.get(groupPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.activity_home_post, null);
}
TextView postView = (TextView) convertView.findViewById(R.id.post);
postView.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
postView.setText(post.getText());
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public void updateReceiptsList (List<Post> postList) {
this.posts.clear();
this.posts.addAll(postList);
Log.e(":size",String.valueOf(postList.size()));
Log.e("checking", postList.toString());
for(Post p: postList) {
Log.e("post", p.toString());
}
this.notifyDataSetChanged();
Log.e("checking", "here");
}
}
答案 0 :(得分:0)
此适配器丢失对您的列表的引用。 notifyDataSetChanged()将不起作用。
expandableListAdapter = ExpandableListAdapter(context,postList); expandableListAdapter.notifyDataSetChanged();
请再读一遍。