解析xml url以卡的形式显示

时间:2014-02-18 09:58:20

标签: android google-glass

我正在尝试解析xml文件并以单独的卡形式显示供稿。这是我的代码到目前为止的样子:

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;
        }
    }
}

我没有收到任何错误,但卡片上没有显示任何内容。我无法弄清楚原因是什么。是因为卡(SeconScreen.this)。但是,如果我不这样做,它会给我一个错误,说构造函数没有定义。

0 个答案:

没有答案