D
D
dearname2014-02-10 22:37:29
Android
dearname, 2014-02-10 22:37:29

AsynTask: why doesn't "sa" see the adapter?

For the second day I have been trying to figure out AsynTask, I don’t understand why "sa" does not see the adapter.

public class CurrencyListParsing extends ListActivity {
  private final static String KEY_CHAR_CODE = "CharCode";
  private final static String KEY_VALUE = "Value";
  private final static String KEY_NOMINAL = "Nominal";
  private final static String KEY_NAME = "Name";
  ArrayList<Map<String, String>> data;
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String[] from = { KEY_CHAR_CODE, KEY_VALUE, KEY_NOMINAL, KEY_NAME };
    int[] to = { R.id.charCodeView, R.id.valueView, R.id.nominalView,R.id.nameView };
    SimpleAdapter sa = new SimpleAdapter(this, data, R.layout.item_view,from, to);
    new NewThread().execute();
  }
  
  public class NewThread extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... arg) {
      data =new ArrayList<Map<String, String>>();
      
      Map<String, String> m;
      try {
        // Создаем объект URL
        URL url = new URL(getString(R.string.rates_url));
        // Соединяемся
        HttpURLConnection httpConnection =(HttpURLConnection) url.openConnection();
        // Получаем от сервера код ответа
        int responseCode = httpConnection.getResponseCode();
        // Если код ответа хороший, парсим поток(ответ сервера),
        // устанавливаем дату в заголовке приложения и
        // заполняем list нужными Map'ами
        if (responseCode == HttpURLConnection.HTTP_OK) {
        InputStream in = httpConnection.getInputStream();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document dom = db.parse(in);
        org.w3c.dom.Element docElement = dom.getDocumentElement();
        String date = docElement.getAttribute("Date");
        setTitle(getTitle() + " на " + date);
        NodeList nodeList = docElement.getElementsByTagName("Valute");
        int count = nodeList.getLength();
        if (nodeList != null && count > 0) {
        for (int i = 0; i < count; i++) {
        Element entry = (Element) nodeList.item(i);
        m = new HashMap<String, String>();
        String charCode = ((Document) entry).getElementsByTagName(KEY_CHAR_CODE).item(0).getFirstChild().getNodeValue();
        String value = ((Document) entry).getElementsByTagName(KEY_VALUE).item(0).getFirstChild().getNodeValue();
        String nominal = "за " + ((Document) entry).getElementsByTagName(KEY_NOMINAL).item(0).getFirstChild().getNodeValue();
        String name = ((Document) entry).getElementsByTagName(KEY_NAME).item(0).getFirstChild().getNodeValue();
          m.put(KEY_CHAR_CODE, charCode);
          m.put(KEY_VALUE, value);
          m.put(KEY_NOMINAL, nominal);
          m.put(KEY_NAME, name);
          data.add(m);
      }
      }
      } else {
        }
        } catch (MalformedURLException e) {
        e.printStackTrace();
        } catch (IOException e) {
        e.printStackTrace();
        } catch (ParserConfigurationException e) {
        e.printStackTrace();
        } catch (SAXException e) {
        e.printStackTrace();
      }
    
  return null;
    }
    @Override
    protected void onPostExecute(String result) {
      setListAdapter(sa);
    }
  }
}

Please note, here is only an error, <"sa" cannot be resolved to a varible>
protected void onPostExecute(String result) {
      setListAdapter(sa);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mintormo, 2014-02-11
@dearname

Why be surprised? You have an adapter declared in the onCreate method

...
SimpleAdapter sa = new SimpleAdapter(this, data, R.layout.item_view,from, to);
...

That is, the sa variable is local to onCreate: it is not visible in other methods. You need to declare sa as a field of this class: move the declaration to the same place as data. That is, like this:
...
    ArrayList<Map<String, String>> data;
    SimpleAdapter sa;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] from = { KEY_CHAR_CODE, KEY_VALUE, KEY_NOMINAL, KEY_NAME };
        int[] to = { R.id.charCodeView, R.id.valueView, R.id.nominalView,R.id.nameView };
        sa = new SimpleAdapter(this, data, R.layout.item_view,from, to);
        new NewThread().execute();
    }
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question