Advertisement
Guest User

Untitled

a guest
May 5th, 2016
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.81 KB | None | 0 0
  1. package com.tw.vp;
  2.  
  3.  
  4. import android.app.ProgressDialog;
  5. import android.content.Intent;
  6. import android.os.AsyncTask;
  7. import android.os.Bundle;
  8. import android.support.v4.app.Fragment;
  9. import android.support.v4.app.ListFragment;
  10. import android.util.Log;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.widget.AdapterView;
  15. import android.widget.LinearLayout;
  16. import android.widget.ListAdapter;
  17. import android.widget.ListView;
  18. import android.widget.SimpleAdapter;
  19. import android.widget.TextView;
  20.  
  21. import org.json.JSONArray;
  22. import org.json.JSONException;
  23. import org.json.JSONObject;
  24.  
  25. import java.util.ArrayList;
  26. import java.util.HashMap;
  27.  
  28.  
  29. /**
  30.  * A simple {@link Fragment} subclass.
  31.  */
  32. public class VFragment extends ListFragment {
  33.  
  34.     private ProgressDialog pDialog;
  35.  
  36.     // URL to get contacts JSON
  37.     private static String url = "http://api.androidhive.info/contacts/";
  38.  
  39.     // JSON Node names
  40.     private static final String TAG_CONTACTS = "contacts";
  41.     private static final String TAG_ID = "id";
  42.     private static final String TAG_NAME = "name";
  43.     private static final String TAG_EMAIL = "email";
  44.     private static final String TAG_ADDRESS = "address";
  45.     private static final String TAG_GENDER = "gender";
  46.     private static final String TAG_PHONE = "phone";
  47.     private static final String TAG_PHONE_MOBILE = "mobile";
  48.     private static final String TAG_PHONE_HOME = "home";
  49.     private static final String TAG_PHONE_OFFICE = "office";
  50.  
  51.     // contacts JSONArray
  52.     JSONArray contacts = null;
  53.  
  54.     // Hashmap for ListView
  55.     ArrayList<HashMap<String, String>> contactList;
  56.  
  57.  
  58.    // public VFragment() {
  59.         // Required empty public constructor
  60.     //}
  61.  
  62.  
  63.     @Override
  64.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  65.                              Bundle savedInstanceState) {
  66.         // Inflate the layout for this fragment
  67.         View v= inflater.inflate(R.layout.fragment_v, container, false);
  68.         ListView lv =(ListView)v.findViewById(android.R.id.list);
  69.  
  70.         contactList = new ArrayList<HashMap<String, String>>();
  71.  
  72.        
  73.  
  74.         // Listview on item click listener
  75.         lv.setOnItemClickListener( new AdapterView.OnItemClickListener() {
  76.  
  77.             @Override
  78.             public void onItemClick(AdapterView<?> parent, View view,
  79.                                     int position, long id) {
  80.  
  81.                 Log.d("Aufruf", "onItemClick");
  82.                 // getting values from selected ListItem
  83.                 String name = ((TextView) view.findViewById(R.id.name))
  84.                         .getText().toString();
  85.                 String cost = ((TextView) view.findViewById(R.id.email))
  86.                         .getText().toString();
  87.                 String description = ((TextView) view.findViewById(R.id.mobile))
  88.                         .getText().toString();
  89.  
  90.                 // Starting single contact activity
  91.                 Intent in = new Intent(getActivity(), DetailActivity.class);
  92.                 in.putExtra(TAG_NAME, name);
  93.                 in.putExtra(TAG_EMAIL, cost);
  94.                 in.putExtra(TAG_PHONE_MOBILE, description);
  95.                 startActivity(in);
  96.  
  97.             }
  98.         });
  99.  
  100.         // Calling async task to get json
  101.         new GetContacts().execute();
  102.  
  103.  
  104.         return v;
  105.     }
  106.  
  107.     /**
  108.      * Async task class to get json by making HTTP call
  109.      * */
  110.     private class GetContacts extends AsyncTask<Void, Void, Void> {
  111.  
  112.         @Override
  113.         protected void onPreExecute() {
  114.             super.onPreExecute();
  115.             // Showing progress dialog
  116.             pDialog = new ProgressDialog(getActivity());
  117.             pDialog.setMessage("Please wait...");
  118.             pDialog.setCancelable(false);
  119.             pDialog.show();
  120.  
  121.         }
  122.  
  123.         @Override
  124.         protected Void doInBackground(Void... arg0) {
  125.             // Creating service handler class instance
  126.             ServiceHandler sh = new ServiceHandler();
  127.  
  128.             // Making a request to url and getting response
  129.             String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
  130.  
  131.             Log.d("Response: ", "> " + jsonStr);
  132.  
  133.             if (jsonStr != null) {
  134.                 try {
  135.                     JSONObject jsonObj = new JSONObject(jsonStr);
  136.  
  137.                     // Getting JSON Array node
  138.                     contacts = jsonObj.getJSONArray(TAG_CONTACTS);
  139.  
  140.                     // looping through All Contacts
  141.                     for (int i = 0; i < contacts.length(); i++) {
  142.                         JSONObject c = contacts.getJSONObject(i);
  143.  
  144.                         String id = c.getString(TAG_ID);
  145.                         String name = c.getString(TAG_NAME);
  146.                         String email = c.getString(TAG_EMAIL);
  147.                         String address = c.getString(TAG_ADDRESS);
  148.                         String gender = c.getString(TAG_GENDER);
  149.  
  150.                         // Phone node is JSON Object
  151.                         JSONObject phone = c.getJSONObject(TAG_PHONE);
  152.                         String mobile = phone.getString(TAG_PHONE_MOBILE);
  153.                         String home = phone.getString(TAG_PHONE_HOME);
  154.                         String office = phone.getString(TAG_PHONE_OFFICE);
  155.  
  156.                         // tmp hashmap for single contact
  157.                         HashMap<String, String> contact = new HashMap<String, String>();
  158.  
  159.                         // adding each child node to HashMap key => value
  160.                         contact.put(TAG_ID, id);
  161.                         contact.put(TAG_NAME, name);
  162.                         contact.put(TAG_EMAIL, email);
  163.                         contact.put(TAG_PHONE_MOBILE, mobile);
  164.  
  165.                         // adding contact to contact list
  166.                         contactList.add(contact);
  167.                     }
  168.                 } catch (JSONException e) {
  169.                     e.printStackTrace();
  170.                 }
  171.             } else {
  172.                 Log.e("ServiceHandler", "Couldn't get any data from the url");
  173.             }
  174.  
  175.             return null;
  176.         }
  177.  
  178.         @Override
  179.         protected void onPostExecute(Void result) {
  180.             super.onPostExecute(result);
  181.             // Dismiss the progress dialog
  182.             if (pDialog.isShowing())
  183.                 pDialog.dismiss();
  184.             /**
  185.              * Updating parsed JSON data into ListView
  186.              * */
  187.             ListAdapter adapter = new SimpleAdapter(
  188.                     getActivity(), contactList,
  189.                     R.layout.list_item,
  190.                     new String[] { TAG_NAME, TAG_EMAIL,
  191.                     TAG_PHONE_MOBILE },
  192.                     new int[] { R.id.name,
  193.                     R.id.email, R.id.mobile });
  194.  
  195.             setListAdapter(adapter);
  196.         }
  197.  
  198.     }
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement