变量'adapter'中的问题并声明final。我的IDE继续告诉“适配器”需要被声明为final,我在哪里以及如何做到这一点?这让我站起来了,我似乎无法继续前进。请帮忙。它在这里引用了这段代码和问题:Android ImageView from ListView
package com.example.androidhive;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Intent;
import android.widget.TextView;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class CustomizedListView extends Activity {
// All static variables
static final String URL = "http://imforchange.org/international-movement-for-change/testing/data.xml";
// XML node keys
static final String KEY_EVENT = "event"; // parent node
static final String KEY_ID = "eventId";
static final String KEY_TITLE = "eventName";
static final String KEY_DESCRIPTION = "eventDescription";
static final String KEY_ABSTRACT = "eventAbstract";
static final String KEY_DATE = "eventDate";
static final String KEY_PRICE = "eventPrice";
static final String KEY_LINK = "eventLink";
static final String KEY_ADDRESS = "eventAddress";
static final String KEY_CUSTOM_LINK = "eventCustomlink";
static final String KEY_PICTURE = "eventPicture";
static final String KEY_ICON = "eventIcon";
ListView list;
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
LazyAdapter adapter = new LazyAdapter(this, songsList);
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_EVENT);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_DESCRIPTION, parser.getValue(e, KEY_DESCRIPTION));
map.put(KEY_ABSTRACT, parser.getValue(e, KEY_ABSTRACT));
map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
map.put(KEY_PRICE, parser.getValue(e, KEY_PRICE));
map.put(KEY_LINK, parser.getValue(e, KEY_LINK));
map.put(KEY_ADDRESS, parser.getValue(e, KEY_ADDRESS));
//map.put(KEY_CUSTOM_LINK, parser.getValue(e, KEY_CUSTOM_LINK));
map.put(KEY_PICTURE, parser.getValue(e, KEY_PICTURE));
map.put(KEY_ICON, parser.getValue(e, KEY_ICON));
// adding HashList to ArrayList
songsList.add(map);
}
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, songsList);
list.setAdapter(adapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Get the item that was clicked
HashMap<String, String> map = adapter.getItem(position);
// Starting new intent
Intent in = new Intent(getApplicationContext(), DetailView.class);
in.putExtra(KEY_TITLE, map.get(KEY_TITLE));
in.putExtra(KEY_PICTURE, map.get(KEY_PICTURE));
in.putExtra(KEY_DESCRIPTION, map.get(KEY_DESCRIPTION));
in.putExtra(KEY_PRICE, map.get(KEY_PRICE));
in.putExtra(KEY_LINK, map.get(KEY_LINK));
startActivity(in);
}
});
}
}
答案 0 :(得分:1)
你没有发布你的代码声明你的变量“adapter”,但只是在它前面放了一个“final”修饰符,你应该没问题。
答案 1 :(得分:1)
当您尝试在Overridden方法中访问外部变量时,例如“onItemClick”方法,您需要将该变量设为final。通过在变量声明前放置关键字“final”可以轻松完成。另一种方法是使该变量成为成员变量。查看此帖子以了解有关成员变量的更多信息。 https://stackoverflow.com/a/9332462/661074
答案 2 :(得分:0)