2

Problems in variable 'adapter' and declaring final. My IDE keeps on telling 'adapter' needs to be declared as final, where and how to i do it? This had made me come to a stand still i cant seem to move on from there. please help. Its in reference to this code here and problem: 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);
                }
        });     
    }
}
Community
  • 1
  • 1
ejobity
  • 305
  • 1
  • 3
  • 13

3 Answers3

1

You didn't post your code declaring your variable "adapter", but just put a "final" modifier in front of it and you should be fine.

  • it is in reference to this code here: http://stackoverflow.com/questions/11920687/android-imageview-from-listview – ejobity Oct 05 '12 at 19:16
  • the difference in what you posted is that you are accessing the "adapter" variable in your click listener, the link you posted is not. If you access a variable from inside an anonymous class, it has to be declared final. – Russell Christensen Oct 05 '12 at 19:19
  • So make it 'getView(final int position...)'. Might look a bit strange to you but it's legal. For an explanation on why this final is required, see http://stackoverflow.com/questions/4732544/why-are-only-final-variables-accessible-in-anonymous-class – aamit915 Oct 05 '12 at 19:19
  • @ejobity Don't do that. Paste the relevant code in your question. – Lews Therin Oct 05 '12 at 19:19
  • @Lewis Therin Hey Lewis, not to be antagonistic, but how come you don't recommend simply putting `final` in the getView(...) declaration ? – aamit915 Oct 05 '12 at 19:25
1

When you are trying to access an external variable in a Overridden method, such as the "onItemClick" method you are required to make that variable final. Its easily done by placing the keyword "final" in front of the variable declaration. Another way is to make that variable a member variable. Check out this post to learn more about member variables. https://stackoverflow.com/a/9332462/661074

Community
  • 1
  • 1
Kent Andersen
  • 2,151
  • 3
  • 21
  • 29
0

You have three choices here:

  • Make variable adapter a field

  • Make variable adapter a final local variable

  • Copy variable adapter to another final variable and use that new final variable from within your inner class.

This Link explains it some more

Community
  • 1
  • 1
RNJ
  • 15,272
  • 18
  • 86
  • 131