1

I'm trying to create a generic spinner adapter that can bind to a list of objects from a database, having id and name properties, so that I can 1) get the id corresponding to the value selected in the spinner; and 2) set the selected item based on the object id, not the position, as ids won't match the positions in the spinner:

id name
-- -----
1  Alice
3  Bob

The following code for a custom adapter found here is a good start and is generic:

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class SpinAdapter<T> extends ArrayAdapter<T> {
private Context context;
private List<T> values;

public SpinAdapter(Context context, int textViewResourceId, List<T> values) {
    super(context, textViewResourceId, values);
    this.context = context;
    this.values = values;
}

public int getCount() {
    return values.size();
}

public T getItem(int position) {
    return values.get(position);
}

public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    TextView label = new TextView(context);
    label.setTextColor(Color.BLACK);
    label.setText(values.toArray(new Object[values.size()])[position]
            .toString());
    return label;
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    TextView label = new TextView(context);
    label.setTextColor(Color.BLACK);
    label.setText(values.toArray(new Object[values.size()])[position]
            .toString());

    return label;
}
}

In order to set the selected value, I need the adapter to return the position for an object given its id. I was thinking adding a method such as public int getPosition (long id) looping through all the items in the list and returning the index of the one having the correct id.

However, as a beginner, I'm not quite at ease with generic types, abstract types and interfaces. I tried replacing the generic <T> by an abstract class DatabaseItem such as:

public abstract class DatabaseItem  {
    public abstract long getId();
    public abstract String getName();
}

And making my object class extend DatabaseItem, but I don't think this works. Any help would be appreciated.

Community
  • 1
  • 1
phme
  • 21
  • 7
  • A Master adapter to control all others. A legendary goal. I think that forcing all your "adaptable" models to `extends` a class is dangerous. You should consider convert `DatabaseItem` to an `interface` instead. – Héctor Apr 16 '15 at 12:01
  • All for it, if it can work. I just wish I knew how to call a method I only define in the interface -- say getId() -- within the (generic) adapter class. – phme Apr 16 '15 at 17:40

1 Answers1

0

Following bigdestroyers' comment, I used an interface:

public interface DatabaseItem  {
    public abstract long getId();
    public abstract String getName();
}

Then added the following method in the custom adapter:

public int getPosition(long id){
    int position = -1;
    for(T v : values) {
        if(((DatabaseItem)v).getId() == id) {
            position = values.indexOf(v);
        }
    }
    return position;
}

Not sure that's the best or the more elegant way to do, but it seems to do the trick.

phme
  • 21
  • 7
  • Instead of `T` the type parameter must be `T extends DatabaseItem`. So you don't have to cast `v`, just call `getId`. The goal of this is to avoid casting – Héctor Apr 17 '15 at 05:57