我正在尝试通过循环访问一些请求的JSON来填充我的布局(我使用Retrofit)。
当我尝试手动填充布局时(如下所示),它显示正常:
Post post1 = new Post("1", "1", "This is a message.");
但是如果我尝试用所请求的JSON数据填充它,布局就不会被填充,也不会显示在我的屏幕上。只有“这是一条消息”的布局。显示。
以下是我的片段onCreateView()
中的代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
ListView listView = (ListView) view.findViewById(R.id.listview_posts);
final ArrayList<Post> arrayOfUsers = new ArrayList<Post>();
// This works fine. It populates the layout as it should.
Post post1 = new Post("1", "1", "This is a message.");
arrayOfUsers.add(post1);
final RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(BASE_URL)
.build();
final ApiEndpointInterface apiService = restAdapter.create(ApiEndpointInterface.class);
apiService.getJsonStuff(1, new Callback<PostData>() {
@Override
public void success(PostData postData, Response response) {
// This doesn't work either
Post post2 = new Post("1", "1", "This is a message2.");
arrayOfUsers.add(post2);
for (Post p : postData.data) {
// This does not work. The layout isn't populated nor does it display.
Post posty = new Post(p.getId(), p.getUserId(), p.getContent());
arrayOfUsers.add(posty);
// The JSON is being read correctly, since this prints out the right values.
Log.d("MESSAGE", p.getMessage());
}
}
@Override
public void failure(RetrofitError retrofitError) {
retrofitError.printStackTrace();
}
});
PostAdapter adapter = new PostAdapter(getActivity(), arrayOfUsers);
listView.setAdapter(adapter);
return view;
}
回调:
void getJsonStuff(@Path("user_id") int userId, Callback<PostData> response);
Post
型号:
import java.util.ArrayList;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Post {
@Expose
private String id;
@SerializedName("user_id")
@Expose
private String userId;
@Expose
private String content;
public Post(String id, String userId, String content) {
this.id = id;
this.userId = userId;
this.content = content;
}
/**
*
* @return
* The id
*/
public String getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(String id) {
this.id = id;
}
/**
*
* @return
* The userId
*/
public String getUserId() {
return userId;
}
/**
*
* @param userId
* The user_id
*/
public void setUserId(String userId) {
this.userId = userId;
}
/**
*
* @return
* The content
*/
public String getContent() {
return content;
}
/**
*
* @param content
* The content
*/
public void setContent(String content) {
this.content= content;
}
}
PostData
型号:
import java.util.ArrayList;
import java.util.List;
import com.google.gson.annotations.Expose;
public class PostData {
@Expose
public Boolean success;
@Expose
public List<Post> data = new ArrayList<Post>();
/**
*
* @return
* The success
*/
public Boolean getSuccess() {
return success;
}
/**
*
* @param success
* The success
*/
public void setSuccess(Boolean success) {
this.success = success;
}
/**
*
* @return
* The data
*/
public List<Post> getData() {
return data;
}
/**
*
* @param data
* The data
*/
public void setData(List<Post> data) {
this.data = data;
}
}
答案 0 :(得分:1)
在适合您的方案中 - &gt;你按顺序做事:创建Post对象 - 将它添加到列表中 - 根据非空列表创建适配器 - 在列表中设置适配器。
在不起作用的场景中,你是异步执行它们:创建空列表 - 触发数据请求(但还没有数据) - 创建适配器 - 在列表中设置适配器 - 在某个未确定的时刻未来的数据到来了。问题是,在这种情况下,适配器不知道任何更改,因此您需要通知它(在success
回调结束时):
adapter.notifyDataSetChanged()
答案 1 :(得分:0)
您的getJsonStuff
方法应该声明为......
getJsonStuff(int id, Callback<List<Post>> callback)