Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to do Jsonrequest on main thread or to run adapter view after the request in Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 546
    Answer it
    1. @Override
    2. public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
    3. Bundle savedInstanceState) {
    4.  
    5. Bundle args = getArguments();
    6. double lats = args.getDouble(LATS, Lats);
    7. double logs = args.getDouble(LOGS, Logs);
    8. final Bundle bundle = new Bundle();
    9. bundle.putDouble(RestaurantAdapter.LATS, lats);
    10. bundle.putDouble(RestaurantAdapter.LOGS, logs);
    11. StringBuilder stringBuilder = new StringBuilder();
    12. stringBuilder.append("https://maps.googleapis.com/maps/api/place/textsearch/json?");
    1. RequestQueue requestQueue = Volley.newRequestQueue(RestaurantFragment.this.getContext().getApplicationContext());
    2. JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET, stringBuilder.toString(),null,
    3. new Response.Listener<JSONObject>() {
    4. @Override
    5. public void onResponse(JSONObject response) {
    6. try {JSONArray jsonObject = response.getJSONArray("results");
    7. for (int i = 0; i <= response.length(); i++) {
    8. JSONObject jsonObject1 = jsonObject.getJSONObject(i);
    9. String name = jsonObject1.get("name").toString();
    10. String place = jsonObject1.get("formatted_address").toString();
    11. objforRest objforRest = new objforRest(name,place);
    12. arrayList1.add(objforRest);
    13. }
    14.  
    15. Toast toast = Toast.makeText(RestaurantFragment.this.getContext(),arrayList1.toString(),Toast.LENGTH_LONG);
    16. toast.show();
    17.  
    18. } catch (JSONException e) {
    19. e.printStackTrace();
    20. }
    21. }
    22. },new Response.ErrorListener() {
    23. @Override
    24. public void onErrorResponse(VolleyError error) { }
    25. });

    requestqueue.add(objectRequestj);

    1. RecyclerView videoRecycler = (RecyclerView) inflater.inflate(
    2. R.layout.fragment_restaurant2, container, false);
    3. videoRecycler.setHasFixedSize(true);
    4. RestaurantAdapter adapter = new RestaurantAdapter(bundle, RestaurantFragment.this.getContext(),arrayList1);
    5. videoRecycler.setAdapter(adapter);
    6. LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    7. videoRecycler.setLayoutManager(layoutManager);
    8. return videoRecycler;
    9.  
    10. }

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: