我正在使用帶有自定義適配器的ExpandableListView,並且正在調用重置我的適配器中的對象列表的方法。我從一個從API檢索數據的異步任務傳遞該列表。當我在適配器方法中檢查新傳入的列表時,它顯示列表大小爲零。在通過之前的異步分析中不是。 發生了什麼? 在異步:可擴展列表視圖,數據不刷新
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");
}
}
確保正確解析JSON。發佈Log.e(「array:」,jsonArray.toString())'和Post'類的輸出。 –
E/array :: [{「uid」:「00128」,「Comment」:[],「postid」:36,「text」:「我玩很多遊戲」,「timestamp」:「2017-10 -07 17:39:52.358813「},{」uid「:」00128「,」評論「:[],」postid「:35,」text「:」我不學很多「,」timestamp「 10-07 17:39:52.358022「}] Post的輸出是正確的。我已經重寫了toString()方法來顯示文本部分:「我玩很多遊戲」,「我不學很多」 –