Advertisement
Guest User

OnTvRss

a guest
Mar 14th, 2012
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.21 KB | None | 0 0
  1. public class OnTvRss extends LinearLayout implements OnItemClickListener{
  2.  
  3.     // XMLParser parser;
  4.     static ArrayList<String> headlines, des, date, links, images;
  5.     static String GerUlr = null;
  6.     SpecialAdapter adapter;
  7.     static ListView list;
  8.     String TAG = OnTvRss.class.getSimpleName();
  9.     public static AssetManager manager;
  10.     static Context con;
  11.     LayoutInflater mInflater;
  12.     String onTVurl = "http://feeds.feedburner.com/ontveg";
  13.  
  14.     public OnTvRss(Context context, AttributeSet attr) {
  15.         super(context, attr);
  16.         OnTvRss.con = context;
  17.         OnTvRss.list = new ListView(getContext());
  18.         new walaa().execute(onTVurl);
  19.         OnTvRss.list.setOnItemClickListener( OnTvRss.this);
  20.  
  21.        
  22.  
  23.     }
  24.  
  25.     public OnTvRss(Context context) {
  26.         super(context);
  27.         OnTvRss.con = context;
  28.         OnTvRss.list = new ListView(getContext());
  29.         mInflater = (LayoutInflater) context
  30.                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  31.         new walaa().execute(onTVurl);
  32.         OnTvRss.list.setOnItemClickListener(OnTvRss.this);
  33.  
  34.  
  35.    
  36.  
  37.     }
  38.  
  39.     public InputStream getInputStream(URL url) {
  40.  
  41.         try {
  42.  
  43.             return url.openConnection().getInputStream();
  44.  
  45.         } catch (IOException e) {
  46.  
  47.             return null;
  48.  
  49.         }
  50.  
  51.     }
  52.  
  53.     private class walaa extends AsyncTask<String, Void, ArrayList<String>>  {
  54.  
  55.         @Override
  56.         protected ArrayList<String> doInBackground(String... status) {
  57.  
  58.             OnTvRss.this.addView(OnTvRss.list);
  59.  
  60.             headlines = new ArrayList<String>();
  61.             des = new ArrayList<String>();
  62.             date = new ArrayList<String>();
  63.             links = new ArrayList<String>();
  64.             images = new ArrayList<String>();
  65.  
  66.             try {
  67.  
  68.                 URL url = new URL(status[0]);
  69.  
  70.                 XmlPullParserFactory factory = XmlPullParserFactory
  71.                         .newInstance();
  72.  
  73.                 factory.setNamespaceAware(false);
  74.  
  75.                 XmlPullParser xpp = factory.newPullParser();
  76.  
  77.                 // We will get the XML from an input stream
  78.  
  79.                 xpp.setInput(getInputStream(url), "UTF_8");
  80.  
  81.                 /*
  82.                  * We will parse the XML content looking for the tag. 16
  83.                  * However, the rss feed name also is enclosed in a tag. 17 As
  84.                  * we know, every feed begins with these lines: Feed Name.....
  85.                  * 18 so we should skip the tag which is a child of , 19 and
  86.                  * take in consideration only tag which is a child of 20
  87.                  *
  88.                  * 21 In order to achieve this, we will make use of a boolean
  89.                  * variable. 22
  90.                  */
  91.  
  92.                 boolean insideItem = false;
  93.  
  94.                 // Returns the type of current event: START_TAG, END_TAG, etc..
  95.  
  96.                 int eventType = xpp.getEventType();
  97.  
  98.                 while (eventType != XmlPullParser.END_DOCUMENT) {
  99.  
  100.                     if (eventType == XmlPullParser.START_TAG) {
  101.  
  102.                         if (xpp.getName().equalsIgnoreCase("item")) {
  103.  
  104.                             insideItem = true;
  105.  
  106.                         } else if (xpp.getName().equalsIgnoreCase("title")) {
  107.  
  108.                             if (insideItem)
  109.  
  110.                                 headlines.add(xpp.nextText()); // extract the
  111.                                                                 // headline
  112.  
  113.                         } else if (xpp.getName().equalsIgnoreCase("link")) {
  114.  
  115.                             if (insideItem)
  116.  
  117.                                 links.add(xpp.nextText()); // extract the link
  118.                                                             // of
  119.                                                             // article
  120.  
  121.                         } else if (xpp.getName()
  122.                                 .equalsIgnoreCase("description")) {
  123.  
  124.                             if (insideItem)
  125.  
  126.                                 des.add(xpp.nextText()); // extract the link of
  127.                                                             // article
  128.  
  129.                         }
  130.  
  131.                     } else if (eventType == XmlPullParser.END_TAG
  132.                             && xpp.getName().equalsIgnoreCase("item")) {
  133.  
  134.                         insideItem = false;
  135.  
  136.                     }
  137.  
  138.                     eventType = xpp.next(); // move to next element
  139.  
  140.                 }
  141.  
  142.             } catch (MalformedURLException e) {
  143.  
  144.                 e.printStackTrace();
  145.  
  146.             } catch (XmlPullParserException e) {
  147.  
  148.                 e.printStackTrace();
  149.  
  150.             } catch (IOException e) {
  151.  
  152.                 e.printStackTrace();
  153.  
  154.             }
  155.  
  156.             return links;
  157.         }
  158.  
  159.         @Override
  160.         protected void onPostExecute(ArrayList<String> result) {
  161.             Log.d(TAG, "entered onpostExceute ");
  162.  
  163.             // Binding data
  164.             Log.d(TAG, "" + headlines);
  165.             adapter = new SpecialAdapter(con, headlines, des);
  166.             OnTvRss.list.setAdapter(adapter);
  167.             Log.d(TAG, "entered listener ");
  168.             Log.d(TAG, "  " + list);
  169.        
  170.         }
  171.  
  172.     }
  173.  
  174.     @Override
  175.     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  176.         Uri uri = Uri.parse(links.get(arg2));
  177.  
  178.         Intent intent = new Intent(Intent.ACTION_VIEW);
  179.         intent.setData(uri);
  180.  
  181.         con.startActivity(intent);                 
  182.     }
  183.  
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement