Theo
Theo

Reputation: 3139

RSS Feeder and WebView

I have the following NASA rss feeder code which displays in a list the title and description of the main news. It's done with the XML DOM parsing method and works as I want up to this point.

public class MainActivity extends Activity implements OnItemClickListener {

private class RowItem {
    String title = null;
    String description = null;
    String url = null;

    public RowItem(String title,String description,String url) {
        this.title = title;
        this.description = description;
        this.url = url;
    }
}

private class DownloadXMLTask extends AsyncTask<String, Void, String> {

    ArrayList<RowItem> items = null;

    public DownloadXMLTask(ArrayList<RowItem> items) {
        this.items = items;
    }

    // local helper method
    private String getNodeValue(Element entry, String tag) {
        Element tagElement = (Element) entry.getElementsByTagName(tag).item(0);
        return tagElement.getFirstChild().getNodeValue();
    }

    private String downloadAndParseXML(String url) {
        try {
            InputStream in = new java.net.URL(url).openStream();

            // build the XML parser
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            // parse and get XML elements
            Document dom = db.parse(in);
            Element documentElement = dom.getDocumentElement();

            // we want all XML children called 'item'
            NodeList nodes = documentElement.getElementsByTagName("item");

            // for each 'item' do the following
            for (int i = 0; i < nodes.getLength(); i++) {
                Element entry = (Element) nodes.item(i);

                // get the nodes from 'item' that you need
                String title = getNodeValue(entry, "title");
                String description = getNodeValue(entry, "description");
                //String description = getNodeValue(entry, "description");
                String link = getNodeValue(entry, "link");
                // add them to your list
                items.add(new RowItem(title,description,link));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    protected String doInBackground(String... urls) {
        if (urls.length <= 0)
            return null;
        return downloadAndParseXML(urls[0]);
    }

    protected void onPostExecute(String result) {
        updateList(items);

    }
}

public void updateList(ArrayList<RowItem> items) {
    CustomArrayAdapter adapter = new CustomArrayAdapter(this,
            R.layout.row, items);
    listView.setAdapter(adapter);
}

// class used to have custom view for the list item
private class CustomArrayAdapter extends ArrayAdapter<RowItem> {
    Context context;
    List<RowItem> items;

    private class ViewHolder {
        public TextView title;
        public TextView description;
    }

    public CustomArrayAdapter(Context context, int resource,
            List<RowItem> items) {
        super(context, resource, items);
        this.context = context;
        this.items = items;
    }

    @Override
    public View getView(int position, View rowView, ViewGroup parent) {
        // reuse the rowView if possible - for efficiency and less memory consumption
        if (rowView == null) {

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.row, null);
            // configure view holder
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.title = (TextView) rowView.findViewById(R.id.title);
            viewHolder.description = (TextView) rowView.findViewById(R.id.desc);

            rowView.setTag(viewHolder);
        }
        // set the view
        ViewHolder holder = (ViewHolder) rowView.getTag();
        RowItem item = items.get(position);
        holder.title.setText(item.title);
        holder.description.setText(item.description);


        return rowView;
    }
}

ListView listView;
ArrayList<RowItem> items;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // get the listView and set this to be the onItemClickListener
    listView = (ListView) findViewById(R.id.list);
    listView.setOnItemClickListener(this);

    // create the RowItem list - used for storing one news feed
    items = new ArrayList<RowItem>();

    // start the background task to get the news feed
    //new DownloadXMLTask(items).execute("http://feeds.bbci.co.uk/news/rss.xml");
    new DownloadXMLTask(items).execute("http://www.nasa.gov/rss/dyn/breaking_news.rss");

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
    RowItem item = items.get(position);
    // start browser with the url
    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(item.url));
    startActivity(browserIntent);
    //Intent browserIntent = new Intent(MainActivity.this,Webview.class);
    //startActivity(browserIntent);
  }

}

My question is how to use the WebView in order to display the link on it? Should I create an Intent and pass data to another activity that represents the webview? I don't want to display it with that Uri.parse(..) way.

Thank you,

Theo.

Upvotes: 0

Views: 124

Answers (2)

Theo
Theo

Reputation: 3139

I also tried this and it worked..:)

    Intent browserIntent = new Intent(MainActivity.this, WebActivity.class);
    browserIntent.putExtra("url",item.url);
    startActivity(browserIntent);

And I created a new class putting this inside...

 String url =getIntent().getStringExtra("url");/
 webview= (WebView) findViewById(R.id.webView1);
 webview.getSettings().setJavaScriptEnabled(true);
 webview.setWebViewClient(new WebViewClient());
 webview.getSettings().setBuiltInZoomControls(true);

 webview.getSettings().setLoadWithOverviewMode(true);
 webview.getSettings().setUseWideViewPort(true);
 webview.loadUrl(url);

Upvotes: 0

Akhil
Akhil

Reputation: 6697

One Simple solution can be using TextView & Html.fromHtml() method

String htmlString = "<a href="+link+">"+link+"</a>"
Spanned spanned = Html.fromHtml(htmlString);
textView.setText(spanned);

Upvotes: 1

Related Questions