Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FirstFragment extends Fragment {
- View fragment_view;
- ArrayList<Customer> customers;;
- ProgressBar pb;
- SwipeRefreshLayout srl;
- @Override
- public View onCreateView(
- LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState
- ) {
- // Inflate the layout for this fragment
- View rootView = inflater.inflate(R.layout.fragment_first, container, false);
- fragment_view = rootView;
- pb = (ProgressBar) rootView.findViewById(R.id.progress_horizontal);
- srl = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeRefreshLayout);
- srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
- @Override
- public void onRefresh() {
- load();
- new Handler().postDelayed(new Runnable() {
- @Override public void run() {
- // Stop animation (This will be after 1 seconds)
- srl.setRefreshing(false);
- }
- }, 1000); // Delay in millis
- }
- });
- // Configure the refreshing colors
- srl.setColorSchemeResources(android.R.color.holo_blue_bright,
- android.R.color.holo_green_light,
- android.R.color.holo_orange_light,
- android.R.color.holo_red_light);
- load();
- return rootView;
- }
- public void load(){
- pb.setVisibility(ProgressBar.VISIBLE);
- RequestQueue queue = Volley.newRequestQueue(getContext());
- String url = "[URL API KAMU]";
- JsonObjectRequest jsObjRequest = new JsonObjectRequest(
- Request.Method.GET,
- url,
- null,
- new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject response) {
- String id, nama, telp;
- customers = new ArrayList<>();
- try {
- JSONArray jsonArray = response.getJSONArray("result");
- customers.clear();
- if (jsonArray.length() != 0) {
- for (int i = 0; i < jsonArray.length(); i++) {
- JSONObject data = jsonArray.getJSONObject(i);
- id = data.getString("idcustomer").toString().trim();
- nama = data.getString("namacustomer").toString().trim();
- telp = data.getString("telpcustomer").toString().trim();
- customers.add(new Customer(id, nama, telp ));
- }
- showRecyclerGrid();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- pb.setVisibility(ProgressBar.GONE);
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- // TODO Auto-generated method stub
- Log.d("Events: ", error.toString());
- pb.setVisibility(ProgressBar.GONE);
- Toast.makeText(getContext(), "Please check your connection", Toast.LENGTH_SHORT).show();
- }
- });
- queue.add(jsObjRequest);
- }
- private void showRecyclerGrid(){
- RecyclerView recyclerView = (RecyclerView) fragment_view.findViewById(R.id.rv);
- recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
- CustomerAdapter mAdapter = new CustomerAdapter(getContext(), customers);
- recyclerView.setAdapter(mAdapter);
- recyclerView.setItemAnimator(new DefaultItemAnimator());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement