Advertisement
Guest User

Untitled

a guest
Jun 12th, 2014
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.29 KB | None | 0 0
  1. package com.wongtelu.codenamereminder;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import org.json.JSONArray;
  6. import org.json.JSONObject;
  7.  
  8. import com.android.volley.Request.Method;
  9. import com.android.volley.Response;
  10. import com.android.volley.VolleyError;
  11. import com.android.volley.VolleyLog;
  12. import com.android.volley.toolbox.JsonObjectRequest;
  13. import com.android.volley.toolbox.Volley;
  14. import com.wongtelu.codenamereminder.app.AppController;
  15.  
  16. import android.app.Activity;
  17. import android.app.ProgressDialog;
  18. import android.os.Bundle;
  19. import android.util.Log;
  20. import android.view.LayoutInflater;
  21. import android.view.View;
  22. import android.view.ViewGroup;
  23. import android.widget.BaseAdapter;
  24. import android.widget.ListView;
  25. import android.widget.TextView;
  26.  
  27. public class MainActivity extends Activity {
  28.    
  29.     private String TAG = this.getClass().getSimpleName();
  30.     private ListView listView;
  31.     private ArrayList<NewsModel> arrNews ;
  32.     private LayoutInflater lf;
  33.     private VolleyAdapter va;
  34.     private ProgressDialog pDialog;
  35.    
  36.    
  37.     private String tag_json_obj = "jobj_req";
  38.     private String url = "http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json";
  39.    
  40.  
  41.     @Override
  42.     protected void onCreate(Bundle savedInstanceState) {
  43.         super.onCreate(savedInstanceState);
  44.         setContentView(R.layout.activity_main);
  45.         lf = LayoutInflater.from(this);
  46.        
  47.         arrNews = new ArrayList<NewsModel>();
  48.         va = new VolleyAdapter();
  49.        
  50.         listView = (ListView) findViewById(R.id.listView);
  51.         listView.setAdapter(va);
  52.        
  53.         Volley.newRequestQueue(this);
  54.        
  55.         pDialog = new ProgressDialog(this);
  56.         pDialog.setMessage("Loading...");
  57.         pDialog.setCancelable(false);
  58.        
  59.         //Start Activity
  60.         showProgressDialog();
  61.         JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
  62.                 url, null,
  63.                 new Response.Listener<JSONObject>() {
  64.                     @Override
  65.                     public void onResponse(JSONObject response) {
  66.                         Log.d(TAG, response.toString());
  67.                         parseJSON(response);
  68.                         va.notifyDataSetChanged();
  69.                         hideProgressDialog();
  70.                     }
  71.                 }, new Response.ErrorListener() {
  72.                     @Override
  73.                     public void onErrorResponse(VolleyError error) {
  74.                         VolleyLog.d(TAG, "Error: " + error.getMessage());
  75.                         // hide the progress dialog
  76.                         hideProgressDialog();
  77.                     }
  78.                 });
  79.        
  80.         AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
  81.        
  82.     }
  83.    
  84.     private void parseJSON(JSONObject json) {
  85.         try {
  86.             JSONObject value = json.getJSONObject("value");
  87.             JSONArray items = value.getJSONArray("items");
  88.             for (int i = 0; i < items.length(); i++) {
  89.                 JSONObject item = items.getJSONObject(i);
  90.                 NewsModel nm = new NewsModel();
  91.                 nm.setTitle(item.optString("title"));
  92.                 nm.setDescription(item.optString("description"));
  93.                 nm.setLink(item.optString("link"));
  94.                 nm.setPubDate(item.optString("pubDate"));
  95.                 arrNews.add(nm);
  96.             }
  97.         } catch (Exception e) {
  98.             e.printStackTrace();
  99.         }
  100.     }
  101.    
  102.     private void showProgressDialog() {
  103.         if (!pDialog.isShowing())
  104.             pDialog.show();
  105.     }
  106.  
  107.     private void hideProgressDialog() {
  108.         if (pDialog.isShowing())
  109.             pDialog.hide();
  110.     }
  111.    
  112.     class NewsModel {
  113.         private String title;
  114.         private String link;
  115.         private String description;
  116.         private String pubDate;
  117.  
  118.         void setTitle(String title) {
  119.             this.title = title;
  120.         }
  121.  
  122.         void setLink(String link) {
  123.             this.link = link;
  124.         }
  125.  
  126.         void setDescription(String description) {
  127.             this.description = description;
  128.         }
  129.  
  130.         void setPubDate(String pubDate) {
  131.             this.pubDate = pubDate;
  132.         }
  133.  
  134.         String getLink() {
  135.             return link;
  136.         }
  137.  
  138.         String getDescription() {
  139.             return description;
  140.         }
  141.  
  142.         String getPubDate() {
  143.             return pubDate;
  144.         }
  145.  
  146.         String getTitle() {
  147.  
  148.             return title;
  149.         }
  150.     }
  151.    
  152.     class VolleyAdapter extends BaseAdapter {
  153.  
  154.         @Override
  155.         public int getCount() {
  156.             return arrNews.size();
  157.         }
  158.  
  159.         @Override
  160.         public Object getItem(int i) {
  161.             return arrNews.get(i);
  162.         }
  163.  
  164.         @Override
  165.         public long getItemId(int i) {
  166.             return 0;
  167.         }
  168.  
  169.         @Override
  170.         public View getView(int i, View view, ViewGroup viewGroup) {
  171.             ViewHolder vh ;
  172.            if(view == null) {
  173.                vh = new ViewHolder();
  174.                view = lf.inflate(R.layout.row_listview, null);
  175.                vh.tvTitle = (TextView) view.findViewById(R.id.txtTitle);
  176.                vh.tvDesc = (TextView) view.findViewById(R.id.txtDesc);
  177.                vh.tvDate = (TextView) view.findViewById(R.id.txtDate);
  178.                view.setTag(vh);
  179.           }
  180.             else {
  181.                vh = (ViewHolder) view.getTag();
  182.            }
  183.  
  184.             NewsModel nm = arrNews.get(i);
  185.             vh.tvTitle.setText(nm.getTitle());
  186.             vh.tvDesc.setText(nm.getDescription());
  187.             vh.tvDate.setText(nm.getPubDate());
  188.             return view;
  189.         }
  190.        
  191.         class  ViewHolder {
  192.             TextView tvTitle;
  193.             TextView tvDesc;
  194.             TextView tvDate;
  195.         }
  196.     }  
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement