开发者

How do I only abbreviate the selected item of a Spinner?

the user clicks on the spinner and I provide a bunch of Unit instances presenting the value by using the Unit.toString() method. The method provides a string that contains the full name as well as the abbreviation. After the user has made the choice, I just want to show the abbrev开发者_开发百科iation in the spinner since the output of the toString method is to long.

The initialization in my onCreate method:

this.spinner = (Spinner)this.findViewById(R.id.om_addIngredientDialog_unit);
ArrayAdapter<Unit> adapter = new ArrayAdapter<Unit>(context, R.layout.om_addingredientdialog_spinner, R.id.om_addIngredientDialog_spinner, DAOUnit.getAllArray(this.dbAdapter.getMDb()));
this.spinner.setAdapter(adapter);

here my Unit class:

public class Unit implements Serializable{

    private long id;
    private String name;
    private String abbreviation;

    public Unit(){}

    public Unit(long id, String name, String abbreviation){
        super();
        this.id = id;
        this.name = name;
        this.abbreviation = abbreviation;
    }

    public void setId(long id){
        this.id = id;
    }

    public long getId(){
        return id;
    }

    public void setName(String name){
        this.name = name;
    }

    public String getName(){
        return name;
    }

    public void setAbbreviation(String abbreviation){
        this.abbreviation = abbreviation;
    }

    public String getAbbreviation(){
        return abbreviation;
    }

    @Override
    public String toString(){
        if(this.abbreviation == null || this.abbreviation.length() == 0){
            return this.name;
        }

        StringBuilder sb = new StringBuilder();
        sb.append(this.name).append("(").append(this.abbreviation).append(")");
        return sb.toString();
    }
}

Is there any way I could achieve the mentioned output? Thank you for your help!

Thanks,

Kon


Create your own subclass of ArrayAdapter and override getView(), just like you would with a ListView, to use the abbreviation rather than the normal toString() value.


Thank you! That did the job :)

Here are my changes:

ArrayAdapter<Unit> adapter =new SpinnerArrayAdapter(context, R.layout.om_addingredientdialog_spinner, R.id.om_addIngredientDialog_spinner, DAOUnit.getAllArray(this.dbAdapter.getMDb()));
this.spinner.setAdapter(adapter);

and the adapter:

public class SpinnerArrayAdapter extends ArrayAdapter<Unit>{

    private LayoutInflater inflater = null;
    private int resourceLayout = 0;

    public SpinnerArrayAdapter(Context context, int resourceLayout, int textViewResourceId, Unit[] units){
        super(context, resourceLayout, textViewResourceId, units);
        this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.resourceLayout = resourceLayout;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View view;
        if(convertView == null){
            view = this.inflater.inflate(this.resourceLayout, parent, false);
        }else{
            view = convertView;
        }

        Unit unit = this.getItem(position);
        TextView textView = (TextView)view;
        textView.setText(unit.getAbbreviation());
        return view;
    }
}

Thanks again :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜