简体   繁体   中英

Android custom spinner adapter binding

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM