简体   繁体   中英

AsyncTask does not return a TreeMap

I have a code that just don't work! The AsyncTask needs to return a TreeMap < Integer, List< String > > but through my logging i see it doesn't. It just stucks!

Here is the code:

private class ParseSite extends
        AsyncTask<String, Void, Map<Integer,List<String>>> {
    Map<Integer,List<String>> quotes;
    List<String> tmpInfo;
    List<String> tmpText;

    @Override
    protected Map<Integer,List<String>> doInBackground(String... arg) {
        // List<String> output = new ArrayList<String>();

        quotes = new TreeMap<Integer,List<String>>();

        try {
            HTMLHelper hh = new HTMLHelper(new URL(arg[0]));
            List<TagNode> qText = hh.getContentByClass("content");
            Logger.l(Logger.TAG_DBG, "GOT qText");
            List<TagNode> qInfo = hh.getContentByClass("sm");
            Logger.l(Logger.TAG_DBG, "GOT qInfo");
            tmpInfo = new ArrayList<String>();
            tmpText = new ArrayList<String>();

            for (Iterator<TagNode> iterator = qText.iterator(); iterator
                    .hasNext();) {
                TagNode divElement = iterator.next();
                String tmp = divElement.getText().toString();
                tmp = tmp.replaceAll("&lt;", "<");
                tmp = tmp.replaceAll("&gt;", ">");
                tmp = tmp.replaceAll("&quot;", "" + (char) 34);

                tmpText.add(tmp);
                Log.i(Logger.TAG_INF, tmp);
            }
            Logger.l(Logger.TAG_DBG, "tmpText[" + tmpText.size() + "] Filled");
            quotes.put(1, tmpText);
            for (Iterator<TagNode> iterator = qInfo.iterator(); iterator
                    .hasNext();) {
                TagNode divElement = iterator.next();
                String tmp = divElement.getText().toString();

                tmpInfo.add(tmp);
                Log.i(Logger.TAG_INF, tmp);
            }
            Logger.l(Logger.TAG_DBG, "tmpInfo[" + tmpInfo.size() + "] Filled ");
            quotes.put(2, tmpInfo);
            Logger.l(Logger.TAG_DBG, "quotes Filled");
        } catch (Exception e) {
            e.printStackTrace();
        }
        Logger.l(Logger.TAG_DBG, "Returning quotes...");
        return quotes;
    }

    @SuppressWarnings("unused")
    protected void onPostExecute(List<String> output) {
          pd.dismiss();
          Logger.l(Logger.TAG_DBG, "Setting adapter...");
          listView.setAdapter(new QuotesArrayAdapter(HTMLParser.this,
              quotes));
        }
}

Through LogCat i can see my "Returning quotes..." but i can't see "Setting adapter..." and the ProgressDialog pd does not dismiss.

BTW sorry for my bad english. I'm russian.

The compiler is warning you, but you choose to ignore it:

@SuppressWarnings("unused")

The compiler knows this method is never used, because you've declared it wrong. Change it to:

@Override
protected void onPostExecute(Map<Integer,List<String>> output) {
      pd.dismiss();
      Logger.l(Logger.TAG_DBG, "Setting adapter...");
      listView.setAdapter(new QuotesArrayAdapter(HTMLParser.this,
          quotes));
}

Notice the type of the parameter, and the @Override annotation. Using this annotation will ensure you overrode methods correctly by giving an error if you didn't.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM