Displaying details on card from an async task

94 views Asked by At

I am trying to extract data from an XML using a SAX parser. I have implemented the SAX parser in an Async task. As I am developing for google glass, the codes for android in a list view is not that user friendly compared to a card.

How do I get the parsed information to be displayed on the cards and where should the creation of the cards be at? I tried including them in the async task but it failed.

ActivityClass

public class Dpublic class DpadInputActivity extends Activity {
    ListView lvPcsPost;
    private List<PostValue> mCards;
    private CardScrollView mCardScrollView;
    private Context context;
    static ArrayList<PostValue> postValueArrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
    context = this;

    createCards();

    mCardScrollView = new CardScrollView(this);
    PostBaseAdapter adapter = new PostBaseAdapter(context, mCards);
    mCardScrollView.setAdapter(adapter);
    mCardScrollView.activate();
    setContentView(mCardScrollView);
    setContentView(R.layout.activity_main1);

    new PostAsync().execute();
    }

    class PostAsync extends AsyncTask<Void, Void, Void> {
        ProgressDialog pd;
        XMLHelper helper;

        @Override
        protected void onPreExecute() {
            pd = ProgressDialog.show(DpadInputActivity.this, "xx", "Loading
             posts ...", true, false);
        }

        //reading and parsing
        @Override
        protected Void doInBackground(Void... arg0) {
            helper = new XMLHelper();
            helper.get();
            postValueArrayList = helper.getPostsList();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            PostBaseAdapter postBaseAdapter = new
               PostBaseAdapter(DpadInputActivity.this, postValueArrayList);
            lvPcsPost.setAdapter(postBaseAdapter);
            pd.dismiss();
        }
     }

    private void createCards() {
        mCards = new ArrayList<PostValue>();
        for (int i = 0; i < postValueArrayList.size(); i++) {
            mCards.add(postValueArrayList.get(i));
         }
    }
}

XML Helper Class

public class XMLHelper extends DefaultHandler{
    private String URL_MAIN = "blabla.xml";
    String TAG = "XMLHelper";
    Boolean currTag = false;
    String currTagVal = "";

    private PostValue post = null;
    private ArrayList<PostValue> posts = new ArrayList<PostValue>();

    public ArrayList<PostValue> getPostsList() {
        return this.posts;
    }

    public void get() {
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser mSaxParser = factory.newSAXParser();
            XMLReader mXmlReader = mSaxParser.getXMLReader();
            mXmlReader.setContentHandler(this);

            InputStream mInputStream = new URL(URL_MAIN).openStream();
            mXmlReader.parse(new InputSource(mInputStream));
        } catch (Exception e) {
            // Exceptions can be handled for different types
            // But, this is about XML Parsing not about Exception Handling
           Log.e(TAG, "Exception: " + e.getMessage());
        }
     }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        if (currTag) {
            currTagVal = currTagVal + new String(ch, start, length);
            currTag = false;
        }
    }
    @Override
    public void startElement(String uri, String localName, String qName,
                             Attributes attributes) throws SAXException {
        Log.i(TAG, "TAG: " + localName);
        currTag = true;
        currTagVal = "";
        if (localName.equals("marker"))
            post = new PostValue();
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
         currTag = false;

        if (localName.equalsIgnoreCase("location")) //post_title
             post.setLocation(currTagVal);

        else if (localName.equalsIgnoreCase("devt_type")) //guild
            post.setType(currTagVal);

        else if (localName.equalsIgnoreCase("gpr")) //post_date
            post.setGpr(currTagVal);

        else if (localName.equalsIgnoreCase("marker")) //end of tag; adding    object to list
        posts.add(post);
    }
}

I have kept the URL for the XML confidential for some reasons.

1

There are 1 answers

1
w9jds On

If you are pulling data in an AsyncTask and want to display it on the UI, you need to return the information. You don't have access to the UI thread from inside the AsyncTask.

As for the actual building of the card you need to make a class that implements the CardScrollAdapter. You will need to override the getView method and then populate the view with the data you pass into your adapter. Make sure to also look into ViewHolders as well.