安卓:SimpleAdapter错误 [英] Android:SimpleAdapter error
问题描述
您好,我有这个SimpleAdapter为了在ListView.These通知显示某些通知是的ArrayList
方言。这里是code:
SimpleAdapter适配器=新SimpleAdapter(这一点,地方主义,R.layout.rss_list_item,新的String [] {称号,pubdate的},新的INT [] {R.id .title伪,R.id.pubDate});
最终的ListView LV =(ListView控件)findViewById(android.R.id.list);
lv.setAdapter(适配器);
的事情是,它显示我三个错误更换预定第一个是在方言说地方主义不能被解析为一个变量。
第二和第三个是在
lv.setAdapter(适配器);
一家这样说点语法错误令牌(S),错位结构(S),另一个在上面写着适配器令牌语法错误括号里面的适配器,VariableDeclaratorId预计此令牌后 。我东西第二和第三误差,因为第一误差引起的。你有什么想法如何解决这个问题?Thans很多提前!
编辑:
方言声明和其他活动创建
这里是code:
保护整数doInBackground(虚空...... PARAMS){
ArrayList的<&HashMap的LT;字符串,字符串>>地方主义=新的ArrayList<&HashMap的LT;字符串,字符串>>();
字符串XML = ParseXMLmethods.getXML();
文档的文档= ParseXMLmethods.XMLfromString(XML);
如果(文件!= NULL){
孩子的NodeList = doc.getElementsByTagName(项目);
ZERO_FLAG = FALSE;
如果(children.getLength()== 0){
ZERO_FLAG =真;
publishProgress();
}
的for(int i = 0; I< children.getLength();我++){
HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
元素e =(元)children.item(I)
map.put(称号,ParseXMLmethods.getValue(即称号));
map.put(pubdate的ParseXMLmethods.getValue(即pubdate的));
map.put(链接,ParseXMLmethods.getValue(即链接));
map.put(说明,ParseXMLmethods.getValue(即,说明));
localist.add(地图); }
从您的意见,我想我看到发生了什么事情。它看起来像你的的AsyncTask
是比你的适配器
单独的文件。有几种方法来解决这个问题。一种是使用一个接口
来设置你的适配器
变量一旦你的任务已经完成。
<一个href=\"http://stackoverflow.com/questions/18517400/inner-class-can-access-but-not-update-values-asynctask/18517648#18517648\">See创建一个接口,这个答案
为的AsyncTask
。
有一个简单的方法是使你的的AsyncTask
的内部类的在您的适配器
是,使方言
这个类的成员变量,这样既可以访问它。然后,你可以设置你的适配器
和您的的ListView
在 onPostExecute()
。
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);
}
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()
.
这篇关于安卓:SimpleAdapter错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!