开发者

Custom Spinner doesnt show the value returned from custom Dialog

开发者 https://www.devze.com 2023-03-28 21:47 出处:网络
I\'m creating a custom Spinner that shows a custom Dialog. For that, I created a class called CustomSpinner and another called CustomSpinnerDialog.

I'm creating a custom Spinner that shows a custom Dialog. For that, I created a class called CustomSpinner and another called CustomSpinnerDialog.

The problem is that, in the Spinner, the selected value in the Dialog is not printed. I know that the value is correctly being returned, I've checked it with LogCat and Debug.

And in my custom dialog, I've created an interface for onItemSelectedListener, so my CustomSpinner class implements this interface. The code is below.

In the spinner xml file I pass it like this. It references the following class named CustomSpinner, that extends a 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"/>

Here is the CustomSpinner.java class:

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);
            Custo开发者_如何学PythonmSpinnerDialog 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);
    }   
}

And here is the CustomSpinnerDialog.java class:

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);
    }   
}

Well, the Spinner is started in an Activity like this:

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

What I tried that didnt solve:

In the method onItemSelected(String itemValue) in the class CustomSpinner, I've tried:

this.setPrompt(itemValue); // Didnt solve

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

And I know that this value is being correctly returned.

Thanks in advance.


I simply removed the adapter class declared in CustomSpinner class, also removed this constructor:

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

And instead of creating the spinner like this:

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

I created it like this:

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);  

And the spinner_target.xml passed as the last parameter to the createFromResource() method is a simple xml file that has only a TextView:

<?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"/> 
0

精彩评论

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

关注公众号