如何在旋转器中不显示所选项目?

时间:2012-10-29 09:49:17

标签: android spinner

我想在旋转器中不显示所选项目,我的微调器看起来像这样......!my spinner  background is just arrow which you can see behind the text image description here

我的微调器背景只是你在文本后面可以看到的箭头...请帮助我......提前感谢..

3 个答案:

答案 0 :(得分:2)

我建议您使用ImageView& AlertDialog代替Spinner

只需在ImageView上打开AlertDialog,然后在AlertDialog中显示您的数据,并在AlertDialog中选择您所选项目的setText。

以下是AlertDialog的示例代码。

new AlertDialog.Builder(this)

.setSingleChoiceItems(items,0, new DialogInterface.OnClickListener()
{
    @Override
    public void onClick(DialogInterface dialog, int which) 
    {
        textview.setText(items[which])
    }
})
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int id) {

           }
})
.show();

其中items是数据的String []。请在OnClick的{​​{1}}中调用此代码。

您还可以查看How to add list in dialog

答案 1 :(得分:0)

spinnerSildeMenu
        .setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
                arg0.getChildAt(0).setVisibility(View.GONE);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
});

答案 2 :(得分:0)

要隐藏微调器下拉列表中的任何项目,您需要根据所需条件传递需要隐藏的项目位置。

public class CustomAdapter extends ArrayAdapter<String> {
private List<String> dates;
private int hideItemPostion;

public CustomAdapter (Context context, int resource, List<String> dates) {
    super(context, resource,dates);
    this.dates=dates;
}
public void setItemToHide(int itemToHide)
{
    this.hideItemPostion =itemToHide;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    View v = null;
    if (position == hideItemPostion) {
        TextView tv = new TextView(getContext());
        tv.setVisibility(View.GONE);
        tv.setHeight(0);
        v = tv;
        v.setVisibility(View.GONE);
    }
    else
        v = super.getDropDownView(position, null, parent);
    return v;
}}

设置适配器如下

final CustomAdapter dataAdapter = new CustomAdapter(this,R.layout.spinner_item,dates);
dataAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
dataAdapter.setItemToHide(0);

在从下拉列表中选择某些项目时,还需要更改位置。在您的情况下,所需的项目是选定项目,因此需要将所选位置传递给适配器。

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, final int i, long l) {
    dataAdapter.notifyDataSetChanged();
        mEPGDateSelector.setSelection(i);
        dataAdapter.setItemToHide(i);}

         @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
});