所以我有這個ListFragment,我想在onCreateView裏面運行排除請求。代碼如下排在裏面的listfragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_feed, container, false);
mFeedView = (ListView) mView.findViewById(android.R.id.list);
mNewPost = (FloatingActionButton) mView.findViewById(R.id.new_post);
mNewPost.setColorNormal(getResources().getColor(R.color.post_primary));
mNewPost.setColorPressed(getResources().getColor(R.color.post_pressed));
mNewPost.setColorRipple(getResources().getColor(R.color.post_rippled));
mNewPost.attachToListView(mFeedView);
mNewPost.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent newPost = new Intent(getActivity(), PostActivity.class);
newPost.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
getActivity().startActivity(newPost);
}
});
mReminderList = new ArrayList<Reminder>();
final JsonArrayRequest mRequest = new JsonArrayRequest(getFeedUrl(),
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++){
try {
JSONObject mItem = response.getJSONObject(i);
Reminder mReminder = new Reminder();
mReminder.setFirstname(mItem.getString(TAG_FIRSTNAME));
mReminder.setLastname(mItem.getString(TAG_LASTNAME));
mReminder.setTitle(mItem.getString(TAG_TITLE));
mReminder.setContent(mItem.getString(TAG_CONTENT));
mReminderList.add(mReminder);
} catch (JSONException e){
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
NetworkUtils.getInstance(getActivity().getApplicationContext()).addToRequestQueue(mRequest);
mFeedAdapter = new FeedAdapter(this.getActivity(), mReminderList);
setListAdapter(mFeedAdapter);
return mView;
}
該適配器是好的,JSON響應是好的。問題是mReminderList沒有填充提醒對象。有趣的是,它只發生在我使用Volley的時候。提前致謝!