What is the correct way to get values ​​from Volley's JSON Reset?

I have a JSON request similar to this:

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
        url,
        (String) null,
        new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                roomArray = parseRoomsResponse(response);
                callback.onSuccess(roomArray);
            }
        }, new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {

    }
}

This request is inside the method onCreateView(), and my method parseRoomsResponse()returns to me ArrayListwhich I want to work with internally onCreateView(), so what is the correct way to do this?

+4
source share
1 answer

I know this is normal, I have to handle the infinite onSuccess inner class, is there a way to get the values ​​in onCreateView? - Zoltan Kurtiak

Ahhhhhh ... I see what you're saying now.

, Request , Response, Volley , .
, onCreateView, onCreateView, .

. , , , , , .


- . :

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_layout, container, false);
    ...
    EditText myEditText = (EditText) rootView.findViewById(R.id.response_dependent_edittext);
    myEditText.setText("Loading Data");

    ImageView myImageView = (ImageView) rootView.findViewById(R.id.response_dependent_imageview);
    myImageView.setImageResource(R.drawable.image_placeholder);
    ...

    return rootView;
}

updateViews;

private void updateViews(ArrayList<MyArrayType> myParsedArray) {

    // Extract relevant data from the array
    ...

    // Replace the place holders in the relevant views
    EditText myEditText = (EditText) getView().findViewById(R.id.response_dependent_edittext);
    myEditText.setText(stringFromParsedArray);

    ImageView myImageView = (ImageView) getView().findViewById(R.id.response_dependent_imageview);
    myImageView.setImageDrawable(drawableFromParsedArray);

}

onResponse:

@Override
    public void onResponse(JSONObject response) {
        myParsedArray = parseResponse(response);
        updateViews(myParsedResponse);
        ...
    }
+1

All Articles