Android Spinner Selection会自动选择项目

时间:2015-07-01 04:33:54

标签: android spinner

我在运行时更新微调器。当微调器项目到来时,第一个项目自动选择。我不想要,只需要选择项目选项。

我已经实施了以下一个解决方案:

        if (checkSelect > 1) {
                    if (camera_device_list_array_data != null) {
                        String url = camera_device_list_array_data.get(position).get(AppHelper.CAMERA_RTSP_URL);
                        String selected_name = camera_device_list_array_data.get(position).get(AppHelper.CAMERA_DEVICE_NAME);
                        GlobalConstants.selected_camera_device_id = Integer.parseInt(camera_device_list_array_data.get(position).get(AppHelper.CAMERA_DEVICE_ID));
                        connected_camera_name_spinner_tv.setText(selected_name);
                        if (mediaPlayer != null) {
                            relaMediaPlay();
                        }
//                        path = url;
                        selected_camera_tv.setText(camera_device_list_array_data.get(position).get(AppHelper.CAMERA_DEVICE_IP_ADDRESS));

                        playVideo();

//                        }

                    }

问题在于:

这是按要求工作但第一项未被选中,选择第二项后选择第一项。

感谢任何帮助。谢谢

3 个答案:

答案 0 :(得分:1)

  

创建自定义微调器

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;

public class CustomSpinner extends Spinner {
    OnItemSelectedListener listener;
    private AdapterView<?> lastParent;
    private View lastView;
    private long lastId;

    public CustomSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        initlistner();
    }

    @Override
    public void setSelection(int position) {
        if (position == getSelectedItemPosition() && listener != null) {
            listener.onItemSelected(lastParent, lastView, position, lastId);
        } else {
            super.setSelection(position);
        }

    }

    private void initlistner() {
        // TODO Auto-generated method stub
        super.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                lastParent = parent;
                lastView = view;
                lastId = id;
                if (listener != null) {
                    listener.onItemSelected(parent, view, position, id);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub
                if (listener != null) {
                    listener.onNothingSelected(parent);
                }
            }
        });

    }

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

}
  

设置监听器

private OnItemSelectedListener listener;

listener = new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {}

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    };
  

将侦听器对象传递给自定义侦听器

cusSpinner.setOnItemSelectedEvenIfUnchangedListener(listener);

答案 1 :(得分:1)

尝试按值选择项目:

yourSpinner.setSelection(Postion);

或尝试使用数组适配器中的位置进行选择:

tauntPrices.Clear();

答案 2 :(得分:0)

Best Answer that works for me, pasting it below:

public class MySpinner extends Spinner {

    public MySpinner(Context context)
    { super(context); }

    public MySpinner(Context context, AttributeSet attrs)
    { super(context, attrs); }

    public MySpinner(Context context, AttributeSet attrs, int defStyle)
    { super(context, attrs, defStyle); }

    @Override public void
    setSelection(int position, boolean animate)
    {
        boolean sameSelected = position == getSelectedItemPosition();
        super.setSelection(position, animate);
        if (sameSelected) {
            // Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
            getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
        }
    }

    @Override public void
    setSelection(int position)
    {
        boolean sameSelected = position == getSelectedItemPosition();
        super.setSelection(position);
        if (sameSelected) {
            // Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
            getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
        }
    }
}