Android - 仅在适配器的Spinner中显示1个值

时间:2012-03-07 16:02:05

标签: android

我有一个spinner控件,我从一个继承自BaseAdapter的类填充。我可以使用这个适配器类填充微调器,但它显示2列,我只想要一个。例如,它目前显示:

John Brown                       john@email.com
Sue Fields                       sue@email.com
Mark Twain                       mark.twain@email.com

我希望它只显示:

John Brown
Sue Fields
Mark Twain

这是我的适配器的代码:

public class POCInfoAdapter extends BaseAdapter {

    //Private objects.
    private List<POCInfo> mListPOCInfo;
    private LayoutInflater mInflater;

    //constructor.
    public POCInfoAdapter(Context c, List<POCInfo> list){
        mListPOCInfo = list;

        //create layout inflater.
        mInflater = LayoutInflater.from(c);
    }

    @Override
    public int getCount(){
        return mListPOCInfo.size();
    }

    @Override
    public Object getItem(int position){
        return mListPOCInfo.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        //Get view reference.
        View view = convertView;

        //If view is null.
        if(view == null){
            //Inflate new layout.
            view = mInflater.inflate(R.layout.poc_list, null);

            //create holder.
            ViewHolder holder = new ViewHolder();

            //Find Controls.
            holder.txtFullName = (TextView)view.findViewById(R.id.txtFullName);
            holder.txtEmailAddress = (TextView)view.findViewById(R.id.txtEmailAddress);

            //Set data structure to view.
            view.setTag(holder);
        }

        //Get selected POC Info.
        POCInfo pocInfo = mListPOCInfo.get(position);

        if(pocInfo != null){
            //query data structure.
            ViewHolder holder = (ViewHolder)view.getTag();

            //Set data to display.
            holder.txtFullName.setText(pocInfo.getFullName());
            holder.txtEmailAddress.setText(pocInfo.getEmailAddress());
        }

        return view;
    }

    //Class to hold data structure on view with POC Information.
    static class ViewHolder{
        private TextView txtFullName;
        private TextView txtEmailAddress;
    }
}

以及填充微调器的代码:

Spinner spSpinner = (Spinner)findViewById(R.id.spPOCs);
spSpinner.setAdapter(new POCInfoAdapter(this, DBAdapter.queryAll()));

DBAdapter.queryAll()返回类型为POCInfo的List。 POCInfoAdapter类也被许多其他需要电子邮件地址的活动使用,因此我不能只将getView()更改为仅返回FullName。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

无论您要显示多少项目,包括单选按钮,您都可以在自定义布局中配置它们。并且您可以通过代码

来扩充此布局
view = mInflater.inflate(R.layout.poc_list, null);