Domanda

I've created a custom ArrayAdapter to show the text in a Spinner the way I want to. However upon running the program the spinner lists appears empty.

Here is my code:

public static class CustomAdapter extends ArrayAdapter<String> {

    private int fontsize;
    private int color;
    private Typeface typeface;
    private int bgcolor = Color.rgb(50, 50, 50);
    private int selectedColor = Color.rgb(50, 50, 50);
    private int selected = -1;
    private List<String> data;

    public CustomAdapter(Context context, int resource, Typeface tf, int colour, int fsize, List<String> labels) {
        super(context, resource);
        fontsize = fsize;
        color = colour;
        typeface = tf;
        data = labels;
    }

    public void setBackgroundColor(int bg){
        bgcolor = bg;
    }

    public void setSelectedColor(int color){selectedColor = color;}

    public void setSelectedPos(int p){
        selected = p;
        notifyDataSetChanged();
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        TextView v = (TextView)super.getView(position, convertView, parent);
        SpannableStringBuilder texter = new SpannableStringBuilder();
        Aux.addText(texter,data.get(position),color,fontsize,typeface);
        if (v != null) {
            v.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
            v.setText(texter, TextView.BufferType.SPANNABLE);
            //v.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            if (position == selected) v.setBackgroundColor(selectedColor);
            else v.setBackgroundColor(bgcolor);
        }
        return v;
    }


    public View getDropDownView(int position,  View convertView,  ViewGroup parent) {
        TextView v = (TextView)super.getDropDownView(position, convertView, parent);
        SpannableStringBuilder texter = new SpannableStringBuilder();
        Aux.addText(texter, data.get(position), color, fontsize, typeface);
        if (v != null) {
            v.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
            v.setText(texter, TextView.BufferType.SPANNABLE);
            if (position == selected) v.setBackgroundColor(selectedColor);
            else v.setBackgroundColor(bgcolor);
        }
        return v;
    }

}

I have found someone with the exact same problem, but I cannot understand the code in the solution in order to implement it myself (here: Custom Spinner Adapter) From what I understad I seem to have a layout problem, however I don't understand what is the relationship between the variables make, v and row in the code.

Also I would like to do it without using a reference to an xml layout file, is this possible? If not, could you provide an example of the layout file necessary?

È stato utile?

Soluzione 2

pass "labels" parameter to the super constructor

Altri suggerimenti

Basically, in your getView method, you are inflating the view the wrong way. You want to inflate it by creating a LayoutInflater object and inflating it using that like the other example shows.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top