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.
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.
Comments