Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error:(32, 27) error: incompatible types: Object cannot be converted to long

Tags:

java

android

I am a newbie to Java.

Error:(40, 5) error: method does not override or implement a method from a supertype
Error:(32, 27) error: incompatible types: Object cannot be converted to long

at

    @Override
    public long getItemId(int position) {
        String item = getItem(position);
        return hashMap.get(item);
    }

Full code is given below

package com.javapapers.android.listview;

import android.content.Context;
import android.widget.ArrayAdapter;
import java.util.HashMap;
import java.util.List;

public class SimpleArrayAdapter extends ArrayAdapter {

    Context context;
    int textViewResourceId;
    private static final String TAG = "SimpleArrayAdapter" ;
    HashMap hashMap = new HashMap();

    public SimpleArrayAdapter(Context context, int textViewResourceId,
                              List objects) {
        super(context, textViewResourceId, objects);
        this.context = context;
        this.textViewResourceId = textViewResourceId;
        for (int i = 0; i < objects.size(); ++i) {
            hashMap.put(objects.get(i), i);
        }
    }

    @Override
    public long getItemId(int position) {
        String item = getItem(position);
        return hashMap.get(item);
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public void add(String object){
        hashMap.put(object,hashMap.size());
        this.notifyDataSetChanged();
    }
}
like image 772
user3718359 Avatar asked Jul 10 '14 21:07

user3718359


1 Answers

The problem you are having stems from the fact that hashMap.get(item) returns an Object, but your method signature specifies that you need to return a long.

public long getItemId(int position) {   // you say you will return a long
    String item = getItem(position);
    return hashMap.get(item);           // but try to return an Object
}

There is no way in Java for the JVM to automatically convert any random Object into a long, so the compiler is giving you that error.

There are a couple ways to fix this.

The Wrong Way

The first (bad) way is cast the variable you are getting from the map to long like so:

public long getItemId(int position) {   // you say you will return a long
    String item = getItem(position);
    return (long)hashMap.get(item);     // and you return a long
}

This will make your code compile, but effectively what you are doing here is promising the compiler that you are really, really sure that what you put in the Map is a Long. At this point the compiler will attempt to unbox the Long into a long and return it. If it really was a Long this will work, and if not you will get a ClassCastException thrown.

The Right Way

In newer versions of Java, there is something called Generics. Using Generics you can specify the allowed type of object which can be added to a container when you define it, like so:

// create a HashMap where all keys are Objects and all values are Longs
Map<Object, Long> hashMap = new HashMap<>();

public long getItemId(int position) {   // you say you will return a long
    String item = getItem(position);
    return hashMap.get(item);           // no cast is required
}

Now, the compiler will only allow values of type Long to be stored in your map, and anything you get() from it will automatically be a Long, removing the need (and danger) of casting the return type.

like image 144
azurefrog Avatar answered Oct 25 '22 22:10

azurefrog