在Spinner中选择One Option和SELECT的结果?

时间:2015-03-13 18:14:30

标签: android android-spinner

我正在寻找一种方法来插入文字" - 选择一个选项 - "在一个微调器里面。我有一个在sqlite中选择返回一些结果,我会在spinner上显示这些结果。

我怎样才能显示" - 选择一个选项 - "我的结果?

微调

Spinner spin = (Spinner)findViewById(R.id.mySpinner);
ConsultaClienteVendaProdutoListAdapter adapter = new ConsultaClienteVendaProdutoListAdapter(this, lista);
spin.setAdapter(adapter);

适配器

public class ConsultaClienteVendaProdutoListAdapter extends BaseAdapter{
    private List<Cliente> list;
    private Context context;


    public ConsultaClienteVendaProdutoListAdapter(List<Cliente> list, Context context) {
        this.list = list;
        this.context = context;
    }

    public void changeList(List<Cliente> list){
        this.list = list;
        notifyDataSetChanged();
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        Cliente cliente = list.get(position);
        if (convertView == null) {
            viewHolder = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.consulta_cliente_venda_produto_adapter, parent, false);

            viewHolder.llClienteAdapter = (LinearLayout) convertView.findViewById(R.id.llClienteAdapter);
            viewHolder.tvNome = (TextView) convertView.findViewById(R.id.tvNome);


            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder)convertView.getTag();
        }

        viewHolder.tvNome.setText(cliente.getNome());


        return convertView;
    }


    /** pattern view holder */
    private static class ViewHolder{
        LinearLayout llClienteAdapter;
        TextView tvNome;
    }
}

2 个答案:

答案 0 :(得分:1)

在您的代码中,您有List<Cliente>,您只需添加一个项目&#34;选择一个位置&#34;到该列表的开头,并将列表增加一个以适应它。

或者,更好的方式:

...
@Override
public int getCount() {
    return list.size() + 1;
}
...

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    Cliente cliente;
    if (position != 0) { //Java counts from zero
        cliente = list.get(position - 1);
    }
    ...
    if (position == 0) {
        viewHolder.tvNome.setText("-Select one option-");
    } else {
        viewHolder.tvNome.setText(cliente.getNome());
    }

    return convertView;
}

应该工作,可能需要修改才能正常工作,但这是一般的想法。

答案 1 :(得分:0)

在列表中,使用&#34设置第一个位置;选择一个位置&#34;。

示例:

list.get(0).setClientName("select one position");