I am trying to parse a xml file and display the feeds in form of separate cards. This is what my code looks like till now:
package com.bliss.android.helloworld.main;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import com.google.android.glass.app.Card;
import com.google.android.glass.widget.CardScrollAdapter;
import com.google.android.glass.widget.CardScrollView;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
public class SecondScreen extends Activity {
ArrayList<Card> headlines = new ArrayList<Card>();
ArrayList links = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
new PostTask().execute("http://feeds.pcworld.com/pcworld/latestnews");
CardScrollView csvCardsView = new CardScrollView(this);
csaAdapter cvAdapter = new csaAdapter();
csvCardsView.setAdapter(cvAdapter);
csvCardsView.activate();
setContentView(csvCardsView);
}
private class PostTask extends AsyncTask<String, Integer, String> {
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String url=params[0];
try{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
boolean insideItem = false;
// Returns the type of current event: START_TAG, END_TAG, etc..
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("item")) {
insideItem = true;
} else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem){
Card newCard = new Card(SecondScreen.this);
newCard.setText(xpp.nextText());
headlines.add(newCard); //extract the headline
}
} else if (xpp.getName().equalsIgnoreCase("link")) {
if (insideItem)
links.add(xpp.nextText()); //extract the link of article
}
}else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
insideItem=false;
}
eventType = xpp.next(); //move to next element
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return url;
}
}
private class csaAdapter extends CardScrollAdapter
{
@Override
public int findIdPosition(Object id)
{
return -1;
}
@Override
public int findItemPosition(Object item)
{
return headlines.indexOf(item);
}
@Override
public int getCount()
{
return headlines.size();
}
@Override
public Object getItem(int position)
{
return headlines.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
return headlines.get(position).toView();
}
}
public InputStream getInputStream(URL url) {
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
return null;
}
}
}
I do not get any error but there is nothing displayed on the card. I cant figure out whats the reason. Is it because of Card(SeconScreen.this). But if I dont put that it gives me an error saying constructor not defined.