Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity {
- ArrayList<Dessert> desserts;
- RecyclerView rv;
- ProgressBar pb;
- SwipeRefreshLayout srl;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- pb = (ProgressBar) findViewById(R.id.progress_horizontal);
- srl = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
- srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
- @Override
- public void onRefresh() {
- load();
- new Handler().postDelayed(new Runnable() {
- @Override public void run() {
- srl.setRefreshing(false);
- }
- }, 1000); // Delay in millis
- }
- });
- 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();
- }
- private void showRecyclerGrid(){
- RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv);
- if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
- recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
- }else{
- recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
- }
- DessertAdapter mAdapter = new DessertAdapter(this, desserts);
- recyclerView.setAdapter(mAdapter);
- recyclerView.setItemAnimator(new DefaultItemAnimator());
- }
- public void load(){
- pb.setVisibility(ProgressBar.VISIBLE);
- RequestQueue queue = Volley.newRequestQueue(this);
- String url = "https://www.themealdb.com/api/json/v1/1/filter.php?c=Dessert";
- //Akses HTTP
- JsonObjectRequest jsObjRequest = new JsonObjectRequest(
- Request.Method.GET,
- url,
- null,
- new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject response) {
- String id, meal, photo;
- desserts = new ArrayList<>();
- try {
- //ambil objek meals
- JSONArray jsonArray = response.getJSONArray("meals");
- desserts.clear();
- //masukkan kedalam Arraylist
- if (jsonArray.length() != 0) {
- for (int i = 0; i < jsonArray.length(); i++) {
- JSONObject data = jsonArray.getJSONObject(i);
- id = data.getString("idMeal").toString().trim();
- meal = data.getString("strMeal").toString().trim();
- photo = data.getString("strMealThumb").toString().trim();
- //masukkan kedalam Arraylist
- desserts.add(new Dessert(id, meal, photo ));
- }
- showRecyclerGrid();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- pb.setVisibility(ProgressBar.GONE);
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- pb.setVisibility(ProgressBar.GONE);
- Toast.makeText(MainActivity.this, "Connection problem!", Toast.LENGTH_SHORT).show();
- }
- });
- queue.add(jsObjRequest);
- }
- }
Add Comment
Please, Sign In to add comment