Advertisement
radityakurnianto

Appetizer.java

Mar 4th, 2015
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.31 KB | None | 0 0
  1. package com.ratu.healthyidea;
  2.  
  3. import android.app.Fragment;
  4. import android.app.ProgressDialog;
  5. import android.content.Intent;
  6. import android.os.AsyncTask;
  7. import android.os.Bundle;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.AdapterView;
  12. import android.widget.TextView;
  13.  
  14. import java.util.ArrayList;
  15. import java.util.List;
  16.  
  17. import com.handmark.pulltorefresh.library.PullToRefreshListView;
  18.  
  19. import org.apache.http.NameValuePair;
  20. import org.apache.http.message.BasicNameValuePair;
  21. import org.json.JSONArray;
  22. import org.json.JSONException;
  23. import org.json.JSONObject;
  24.  
  25. /**
  26.  * Created by radityakurnianto on 1/3/15.
  27.  */
  28. public class Appetizer extends Fragment {
  29.     private View view;
  30.     private PullToRefreshListView pullToRefreshListView;
  31.  
  32.     public Appetizer(){}
  33.  
  34.     @Override
  35.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
  36.         view = inflater.inflate(R.layout.contentlistview, container, false);
  37.         pullToRefreshListView = (PullToRefreshListView)view.findViewById(R.id.contentlist);
  38.         new GetAppetizer().execute();
  39.         return view;
  40.     }
  41.  
  42.     private class GetAppetizer extends AsyncTask<Integer, Integer, Integer>{
  43.         ProgressDialog progressDialog;
  44.         JSONParser jsonParser;
  45.         JSONObject jsonObject;
  46.         JSONArray jsonArray;
  47.         String item, ing, ins, ben, cat, pic;
  48.         List<GetSetContent> getSetContents;
  49.         List<NameValuePair> params;
  50.         CustomAdapterContent customAdapterContent;
  51.         int status = 0;
  52.  
  53.         @Override
  54.         public void onPreExecute(){
  55.             progressDialog = new ProgressDialog(getActivity());
  56.             progressDialog.setMessage("Loading appetizer");
  57.             progressDialog.setIndeterminate(true);
  58.             progressDialog.setCancelable(false);
  59.             progressDialog.show();
  60.         }
  61.  
  62.         @Override
  63.         public Integer doInBackground(Integer... args){
  64.             jsonParser = new JSONParser();
  65.             params = new ArrayList<>();
  66.             getSetContents = new ArrayList<>();
  67.  
  68.             try {
  69.                 //
  70.                 params.add(new BasicNameValuePair(Configuration.G_CATEGORIES, "appetizer"));
  71.  
  72.                 // establish connection to API with post method and return json which stored in
  73.                 // jsonObject variable
  74.                 jsonObject = jsonParser.makeHttpRequest(Configuration.GET_CATEGORIES, "POST", params);
  75.  
  76.                 // check if jsonObject is null or has no json
  77.                 if (jsonObject != null) {
  78.                     try {
  79.                         // get the status of json by parsing status key from the API
  80.                         status = jsonObject.getInt(Configuration.G_STATUS);
  81.                         // if json has status 1 then continue to parse another result
  82.                         // else nothing to do with the API
  83.                         if(status == 1) {
  84.                             // move into jsonArray to get values of categories
  85.                             jsonArray = jsonObject.getJSONArray(Configuration.G_CATEGORIES);
  86.                             // loop into the jsonArray
  87.                             for (int i = 0; i < jsonArray.length(); i++){
  88.                                 // get jsonObject from each jsonArray element
  89.                                 jsonObject = jsonArray.getJSONObject(i);
  90.                                 // store each value to variable
  91.                                 item = jsonObject.getString(Configuration.G_ITEMNAME);
  92.                                 ing = jsonObject.getString(Configuration.G_INGREDIENTS);
  93.                                 ins = jsonObject.getString(Configuration.G_INSTRUCTION);
  94.                                 ben = jsonObject.getString(Configuration.G_BENEFIT);
  95.                                 cat = jsonObject.getString(Configuration.G_CAT);
  96.                                 pic = jsonObject.getString(Configuration.G_PICTURE);
  97.                                 // add each value to our custom list
  98.                                 getSetContents.add(new GetSetContent(item, ing, ins, ben, cat, pic, pic));
  99.                             }
  100.                         }
  101.                     }catch (JSONException e){
  102.                         e.printStackTrace();
  103.                     }
  104.                 }
  105.             } catch (Exception e){
  106.                 e.printStackTrace();
  107.             }
  108.             return status;
  109.         }
  110.  
  111.         @Override
  112.         public void onPostExecute(Integer msg){
  113.             super.onPostExecute(msg);
  114.             if(msg == 1) {
  115.                 // dismiss the progress dialog
  116.                 progressDialog.dismiss();
  117.                 // notify listview if we are done doing something
  118.                 pullToRefreshListView.onRefreshComplete();
  119.                 // initializing custom adapter for our listview
  120.                 // we 2 constructor for our custom adapter
  121.                 // @param1 : context = getActivity()
  122.                 // @param2 : custom list = getSetContents
  123.                 customAdapterContent = new CustomAdapterContent(getActivity(), getSetContents);
  124.                 // fill our custom listview with the value from our custom adapter
  125.                 pullToRefreshListView.setAdapter(customAdapterContent);
  126.  
  127.                 // provide tap action for each item in our custom listview
  128.                 // each tap will navigate us to the detail page of each item
  129.                 pullToRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  130.                     @Override
  131.                     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  132.                         // obtaining value from a single item in custom listview
  133.                         String item = ((TextView)view.findViewById(R.id.itemname)).getText().toString();
  134.                         String ing = ((TextView)view.findViewById(R.id.ingredient)).getText().toString();
  135.                         String ins = ((TextView)view.findViewById(R.id.instruction)).getText().toString();
  136.                         String ben = ((TextView)view.findViewById(R.id.benefit)).getText().toString();
  137.                         String cat = ((TextView)view.findViewById(R.id.categories)).getText().toString();
  138.                         String pic = ((TextView)view.findViewById(R.id.imgurl)).getText().toString();
  139.  
  140.                         // our intent has 2 constructor
  141.                         // @param1 : context = getActivity() this is our departure
  142.                         // @param2 : class name = ItemDetail.class this is our destination
  143.                         Intent intent = new Intent(getActivity(), ItemDetail.class);
  144.                         // assigning value to send to the detail page with the help of Intent
  145.                         intent.putExtra("item", item);
  146.                         intent.putExtra("ing", ing);
  147.                         intent.putExtra("ins", ins);
  148.                         intent.putExtra("ben", ben);
  149.                         intent.putExtra("cat", cat);
  150.                         intent.putExtra("pic", pic);
  151.                         // finally start the activity
  152.                         startActivity(intent);
  153.                     }
  154.                 });
  155.             }
  156.         }
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement