Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ratu.healthyidea;
- import android.app.Fragment;
- import android.app.ProgressDialog;
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AdapterView;
- import android.widget.TextView;
- import java.util.ArrayList;
- import java.util.List;
- import com.handmark.pulltorefresh.library.PullToRefreshListView;
- import org.apache.http.NameValuePair;
- import org.apache.http.message.BasicNameValuePair;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- /**
- * Created by radityakurnianto on 1/3/15.
- */
- public class Appetizer extends Fragment {
- private View view;
- private PullToRefreshListView pullToRefreshListView;
- public Appetizer(){}
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
- view = inflater.inflate(R.layout.contentlistview, container, false);
- pullToRefreshListView = (PullToRefreshListView)view.findViewById(R.id.contentlist);
- new GetAppetizer().execute();
- return view;
- }
- private class GetAppetizer extends AsyncTask<Integer, Integer, Integer>{
- ProgressDialog progressDialog;
- JSONParser jsonParser;
- JSONObject jsonObject;
- JSONArray jsonArray;
- String item, ing, ins, ben, cat, pic;
- List<GetSetContent> getSetContents;
- List<NameValuePair> params;
- CustomAdapterContent customAdapterContent;
- int status = 0;
- @Override
- public void onPreExecute(){
- progressDialog = new ProgressDialog(getActivity());
- progressDialog.setMessage("Loading appetizer");
- progressDialog.setIndeterminate(true);
- progressDialog.setCancelable(false);
- progressDialog.show();
- }
- @Override
- public Integer doInBackground(Integer... args){
- jsonParser = new JSONParser();
- params = new ArrayList<>();
- getSetContents = new ArrayList<>();
- try {
- //
- params.add(new BasicNameValuePair(Configuration.G_CATEGORIES, "appetizer"));
- // establish connection to API with post method and return json which stored in
- // jsonObject variable
- jsonObject = jsonParser.makeHttpRequest(Configuration.GET_CATEGORIES, "POST", params);
- // check if jsonObject is null or has no json
- if (jsonObject != null) {
- try {
- // get the status of json by parsing status key from the API
- status = jsonObject.getInt(Configuration.G_STATUS);
- // if json has status 1 then continue to parse another result
- // else nothing to do with the API
- if(status == 1) {
- // move into jsonArray to get values of categories
- jsonArray = jsonObject.getJSONArray(Configuration.G_CATEGORIES);
- // loop into the jsonArray
- for (int i = 0; i < jsonArray.length(); i++){
- // get jsonObject from each jsonArray element
- jsonObject = jsonArray.getJSONObject(i);
- // store each value to variable
- item = jsonObject.getString(Configuration.G_ITEMNAME);
- ing = jsonObject.getString(Configuration.G_INGREDIENTS);
- ins = jsonObject.getString(Configuration.G_INSTRUCTION);
- ben = jsonObject.getString(Configuration.G_BENEFIT);
- cat = jsonObject.getString(Configuration.G_CAT);
- pic = jsonObject.getString(Configuration.G_PICTURE);
- // add each value to our custom list
- getSetContents.add(new GetSetContent(item, ing, ins, ben, cat, pic, pic));
- }
- }
- }catch (JSONException e){
- e.printStackTrace();
- }
- }
- } catch (Exception e){
- e.printStackTrace();
- }
- return status;
- }
- @Override
- public void onPostExecute(Integer msg){
- super.onPostExecute(msg);
- if(msg == 1) {
- // dismiss the progress dialog
- progressDialog.dismiss();
- // notify listview if we are done doing something
- pullToRefreshListView.onRefreshComplete();
- // initializing custom adapter for our listview
- // we 2 constructor for our custom adapter
- // @param1 : context = getActivity()
- // @param2 : custom list = getSetContents
- customAdapterContent = new CustomAdapterContent(getActivity(), getSetContents);
- // fill our custom listview with the value from our custom adapter
- pullToRefreshListView.setAdapter(customAdapterContent);
- // provide tap action for each item in our custom listview
- // each tap will navigate us to the detail page of each item
- pullToRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- // obtaining value from a single item in custom listview
- String item = ((TextView)view.findViewById(R.id.itemname)).getText().toString();
- String ing = ((TextView)view.findViewById(R.id.ingredient)).getText().toString();
- String ins = ((TextView)view.findViewById(R.id.instruction)).getText().toString();
- String ben = ((TextView)view.findViewById(R.id.benefit)).getText().toString();
- String cat = ((TextView)view.findViewById(R.id.categories)).getText().toString();
- String pic = ((TextView)view.findViewById(R.id.imgurl)).getText().toString();
- // our intent has 2 constructor
- // @param1 : context = getActivity() this is our departure
- // @param2 : class name = ItemDetail.class this is our destination
- Intent intent = new Intent(getActivity(), ItemDetail.class);
- // assigning value to send to the detail page with the help of Intent
- intent.putExtra("item", item);
- intent.putExtra("ing", ing);
- intent.putExtra("ins", ins);
- intent.putExtra("ben", ben);
- intent.putExtra("cat", cat);
- intent.putExtra("pic", pic);
- // finally start the activity
- startActivity(intent);
- }
- });
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement