List row onclick to open url from Parsed xml in webview

user3381665

I have a customized list view which parses in a xml file which contains data like so

</issue>
<article>
<id>Arts and Culture</id>
<title>Boycott the Boycott</title>
<author> LouLou Castonguay</author>
<description>There is a rumor going around that no one is going to pitch a theme for next years fashion show. I dont get it!</discription>
<thumb_url>http://graffiti.hostoi.com/00Graffiti00/Photos/Art/show.png</thumb_url>
<key>http://www.google.com</key>
</article>
</issue>

and the list activity is like so

public class CustomizedListView extends Fragment {  // All static variables
    static final String URL = "http://graffiti.hostoi.com/00Graffiti00/lists/00main00.xml";
    // XML node keys
    static final String KEY_SONG = "song"; // parent node
    static final String KEY_ID = "id";
    static final String KEY_ARTIST = "artist";
    static final String KEY_DURATION = "duration";
    static final String KEY_THUMB_URL = "thumb_url";
    static final String KEY_LINK = "link";

    ListView list;
    LazyAdapter adapter;
    ArrayList<HashMap<String, String>> songsList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.news, container, false);



       songsList = new ArrayList<HashMap<String, String>>();

    list=(ListView)  rootView.findViewById(R.id.list);

        new RetrieveXML().execute(URL);

        // Getting adapter by passing xml data ArrayList


        // Click event for single list row                XMLParser parser = new XMLParser();

        // Click event for single list row
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                Intent intent = new Intent(getActivity(), Article.class);
                new Bundle();
                intent.putExtra( "b", songsList.get(position).get(KEY_LINK));
                startActivity(intent);



            }

        });
        return rootView;
    }

    class RetrieveXML extends AsyncTask<String, Void, String> {

        private Exception exception;
        XMLParser parser = new XMLParser();


        protected String doInBackground(String... urls) {
            try {

                return parser.getXmlFromUrl(urls[0]);
            } catch (Exception e) {
                this.exception = e;
                return null;
            }
        }

        protected void onPostExecute(String xml) {
            Document doc = parser.getDomElement(xml); // getting DOM element

            NodeList nl = doc.getElementsByTagName(KEY_SONG);
            // looping through all song nodes <song>
            for (int i = 0; i < nl.getLength(); i++) {
                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();
                Element e = (Element) nl.item(i);
                // adding each child node to HashMap key => value
                map.put(KEY_ID, parser.getValue(e, KEY_ID));
                map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
                map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
                map.put(KEY_THUMB_URL, parser.getValue(e, KEY_ID));
                map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

                // adding HashList to ArrayList
                songsList.add(map);

            }

            adapter=new LazyAdapter(getActivity(), songsList);

            list.setAdapter(adapter);
        }
    }
}

and lastly the webview activity the onclick opens is like this

public class Article extends Activity{
    private WebView webview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.articleview);

        Bundle b = getIntent().getExtras();
        String KEY_LINK = b.getString("b");

        webview = (WebView) findViewById(R.id.webView);
        webview.setWebViewClient(new MyWebViewClient(this));
        webview.getSettings().setAppCacheEnabled(false);
        webview.getSettings().setBuiltInZoomControls(true);
        webview.setInitialScale(1);
        webview.loadUrl(KEY_LINK);



    }


    private class MyWebViewClient extends WebViewClient {
        public MyWebViewClient(Article article) {
        }
    }
}

and everything works perfectly, except the onclick opens a blank activity of the web view rather than load in google.com any ideas on what I'm doing wrong or how to potentially do this? Thanks.

Spring Breaker

You have defined

 static final String KEY_LINK = "link";

and you are sending KEY_LINK to Article Activity which you are subsequently setting the url of the WevView.Since you are not updating the value of KEY_LINK anywhere your WevView results like following,

webview.loadUrl("link"); 

which renders a blank screen.

Hope you are clear now.

EDIT

I am assuming that you want the following url from the xml,

   <key>http://www.google.com</key>

Change the following

static final String KEY_LINK = "link";

to

static final String KEY_LINK = "key";

and add the following to the onPostExecute method for loop where you are parsing the xml,

 map.put(KEY_LINK, parser.getValue(e, KEY_LINK));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Selenium Json and xml read from Url that get method open it with java

How to pass argument from parsed json value to onclick function?

Android - How to open the Hangout app from a Hangout URL displayed in the webview?

Open url in webview - phonegap

How to open url in webview activity

Webview is not loading a PDF from an URL

NativeScript WebView open url in default browser

Open and encrypted url using webview

Open URL from drop down list

open xml Numbered list

Objective-c: Open URL in WebView (Mac)

Simple Displaying a list of Items Parsed from JSON

How to make parsed list items open new class

Call WebView in MainActivity from menu fragment onClick

LinearLayout onclick open WebView not browser

Open URL captured from XML file using Nokogiri

Issue with open Google Map URL in WebView?

How to open a webview from imagebutton?

How to populate textview from parsed xml android

Android Appcelerator Get URL from remote URL in webview and open it in devices default browser or a new webview

I have a url in string.xml file inside <a href tag which has dynamic values. how to open that url in webview?

Android open URL in WebView

Select an option from a dropdown list by onClick on data-table row

List Open by OnClick

Android, Xamarin: Show 3 pictures from list in a row, then open up new row underneath

Android - How to open a fragment from current fragment activity on clicking a URL in WebView?

How do I fill my DataGrid with my C# list containing my XML Data that was parsed from a XML Node Reader?

open row from List located on second TabItem from first TabItem

getting null value from parsed xml file