自定义微调器不显示自定义对话框返回的值

时间:2011-08-18 19:02:57

标签: android android-layout android-widget spinner

我正在创建一个显示自定义Dialog的自定义Spinner。为此,我创建了一个名为CustomSpinner的类,另一个名为CustomSpinnerDialog。

问题在于,在Spinner中,不会打印对话框中的选定值。我知道正确返回了值,我已经使用LogCat和Debug检查了它。

在我的自定义对话框中,我为onItemSelectedListener创建了一个接口,因此我的CustomSpinner类实现了这个接口。代码如下。

在spinner xml文件中,我像这样传递它。它引用了以下名为CustomSpinner的类,它扩展了一个Spinner:

 <com.myproject.CustomSpinner
    android:id="@+id/customSpinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="13dp"
    android:prompt="@string/my_spinner"/>

以下是 CustomSpinner.java 类:

public class CustomSpinner extends Spinner implements DialogInterface.OnClickListener, CustomSpinnerDialog.OnItemSelectedListener
{

    public OnAddListener mListener = null;
    public Context mContext;
    public String[] mDataList;
    public SpinnerAdapter mAdapter = null;


    public interface OnAddListener
    {
        void onAdd(Spinner inSpinner);
    }

    public CustomSpinner(Context context, String[] dataList) 
    {
        super(context); 
        this.mContext = context;
        this.mDataList = dataList;
    }

    public CustomSpinner(Context context, AttributeSet attrs) 
    {
        super(context, attrs);  
        this.mContext = context;
    }

    public void setDataList(String[] dataList)
    {
        this.mDataList = dataList;
    }

    @Override
    public boolean performClick() 
    {   
        boolean handled = false;
        if (!handled) 
        {
            handled = true;                                                

            mAdapter = new SpinnerAdapter(mContext, R.id.spinnerItemText, mDataList);
            CustomSpinnerDialog dialog = new CustomSpinnerDialog(mContext, mAdapter, this);               
            dialog.setOnItemSelectedListener(this);
            dialog.setDialogTitle("My custom Dialog");                 
            dialog.show();
        }        
        return handled;
    }

     @Override
     public void onClick(DialogInterface dialog, int which) 
     {
         if(which == DialogInterface.BUTTON_POSITIVE) 
         {
             if(mListener!=null)
                 mListener.onAdd(this);
         } 
         else 
         {
             setSelection(which);
         }
         dialog.dismiss();
     }

    public class SpinnerAdapter extends ArrayAdapter<String>
    {
        public Context mContext;

        public SpinnerAdapter(Context context, int textViewResourceId, String[] objects) 
        {
            super(context, textViewResourceId, objects);
            this.mContext = context;            
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {           
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.spinnerdialogitem, null);
            TextView item = (TextView) view.findViewById(R.id.spinnerItemText);
            String text = mDataList[position];
            item.setText(text);
            return view;
        }
    }

    @Override
    public void onItemSelected(String itemValue) 
    {       
        int position = mAdapter.getPosition(itemValue);
        this.setSelection(3);
    }   
}

这是 CustomSpinnerDialog.java 类:

public class CustomSpinnerDialog extends Dialog implements OnItemClickListener, View.OnClickListener
{
    private OnItemSelectedListener onItemSelectedListener;

    public DialogInterface.OnClickListener mListener;
    public Context mContext;    

    public interface OnItemSelectedListener
    {
        public void onItemSelected(String itemValue);       
    }

    public CustomSpinnerDialog(Context context, CustomSpinner.SpinnerAdapter spinnerAdapter, DialogInterface.OnClickListener listener) 
    {
        super(context);     
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.setContentView(R.layout.custom_spinner);
        mListener = listener;
        mContext = context;

        ListView listView = (ListView) this.findViewById(R.id.listview);
        listView.setAdapter(spinnerAdapter);
        listView.setOnItemClickListener(this);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);        
    }

    public void setOnItemSelectedListener(OnItemSelectedListener listener)
    {
        this.onItemSelectedListener = listener;
    }

    @Override
    public void onClick(View v) 
    {
        if(mListener != null)
            mListener.onClick(this, DialogInterface.BUTTON_POSITIVE);           
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    {
        if(mListener != null)
            mListener.onClick(this, position);
        String text = (String) parent.getItemAtPosition(position);
        onItemSelectedListener.onItemSelected(text);        
    }

    public void setDialogTitle(String title)
    {
        TextView titleText = (TextView) this.findViewById(R.id.titleText);
        titleText.setText(title);
    }   
}

嗯,Spinner是在这样的Activity中启动的:

cSpinner= (CustomSpinner) findViewById(R.id.customSpinner);     
cSpinner.setDataList(dataList);
cSpinner.setTag("CustomSpinner");
cSpinner.setOnItemSelectedListener(controller);

我尝试过但没有解决的问题:

在CustomSpinner类的方法onItemSelected(String itemValue)中,我尝试过:

this.setPrompt(itemValue); // Didnt solve

int position = mAdapter.getPosition(itemValue);//Didnt solve
this.setSelection(itemValue);

我知道这个值正确返回。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我只是删除了CustomSpinner类中声明的适配器类,也删除了这个构造函数:

public CustomSpinner(Context context, String[] dataList) 
{
   super(context); 
   this.mContext = context;
   this.mDataList = dataList;
}

而不是像这样创建微调器:

cSpinner= (CustomSpinner) findViewById(R.id.customSpinner);     
cSpinner.setDataList(dataList);
cSpinner.setTag("CustomSpinner");
cSpinner.setOnItemSelectedListener(controller);

我是这样创建的:

cSpinner = (CustomSpinner)findViewById(R.id.customSpinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.options_array, R.layout.spinner_target);     
        cSpinner .setAdapter(adapter);
        cSpinner .setTag("CustomSpinner");
        cSpinner .setOnItemSelectedListener(controller);  

作为createFromResource()方法的最后一个参数传递的spinner_target.xml是一个只有TextView的简单xml文件:

<?xml version="1.0" encoding="utf-8"?>
<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spinnerTarget"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:padding="8dp"/>