2

变量“适配器”和声明 final 中的问题。我的 IDE 不断告诉“适配器”需要被声明为最终版本,我在哪里以及如何做?这让我站住了,我似乎无法从那里继续前进。请帮忙。它在这里引用此代码和问题: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);
                }
        });     
    }
}
4

3 回答 3

1

您没有发布声明变量“适配器”的代码,而只是在它前面放了一个“final”修饰符,你应该没问题。

于 2012-10-05T19:12:35.160 回答
1

当您尝试在 Overridden 方法中访问外部变量时,例如“onItemClick”方法,您需要将该变量设为 final。通过将关键字“final”放在变量声明的前面很容易做到这一点。另一种方法是使该变量成为成员变量。查看这篇文章以了解有关成员变量的更多信息。https://stackoverflow.com/a/9332462/661074

于 2012-10-05T19:20:22.510 回答
0

您在这里有三个选择:

  • 使变量适配器成为一个字段

  • 使变量适配器成为最终的局部变量

  • 将变量适配器复制到另一个最终变量,并在内部类中使用该新的最终变量。

这个链接解释了更多

于 2012-10-05T19:24:09.530 回答