Android:SimpleAdapter error

2019-08-29 02:22发布

问题:

Hello i have this SimpleAdapter in order to display some notifications in a ListView.These notifications are in ArrayList localist. here is the code:

SimpleAdapter adapter = new SimpleAdapter(this,localist, R.layout.rss_list_item,new String[] {"title","pubDate"},new int[] {R.id.title, R.id.pubDate });
    final ListView lv = (ListView) findViewById(android.R.id.list);
    lv.setAdapter(adapter);

The thing is that it shows me three errors here.The first one is at localist and says"localist cannot be resolved to a variable".

The second and the third one are at

lv.setAdapter(adapter);

one at the dot that says "Syntax error on token(s), misplaced construct(s)" and the other at the adapter inside the parenthesis that says "Syntax error on token "adapter", VariableDeclaratorId expected after this token". I thing the second and the third error are caused because of the first error. Do you have any ideas how to solve this?Thans a lot in advance!

EDIT: the localist is declared and created in other activity here is the code:

protected Integer doInBackground(Void... params) {
        ArrayList<HashMap<String, String>> localist = new ArrayList<HashMap<String, String>>();
        String xml = ParseXMLmethods.getXML();
        Document doc = ParseXMLmethods.XMLfromString(xml);
        if (doc != null) {
        NodeList children = doc.getElementsByTagName("item");
        ZERO_FLAG = false;
        if (children.getLength() == 0) {
        ZERO_FLAG = true;
        publishProgress();
        }
        for (int i = 0; i < children.getLength(); i++) {
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) children.item(i);
        map.put("title", ParseXMLmethods.getValue(e, "title"));
        map.put("pubDate", ParseXMLmethods.getValue(e, "pubDate"));
        map.put("link", ParseXMLmethods.getValue(e, "link"));
        map.put("description",ParseXMLmethods.getValue(e, "description"));
        localist.add(map);

        }

回答1:

From your comments, I think I see what's going on. It looks like your Asynctask is in a separate file than your Adapter. There are several ways to fix this. One would be to use an interface to set the variable in your Adapter once your task has finished.

See this answer for creating an interface for an AsyncTask.

A simpler way would be to make your AsyncTask an inner class of where your Adapter is and make localist a member variable of that class so that both have access to it. Then you could set your Adapter and your ListView in onPostExecute().