【问题标题】:I am working on android and i am finding difficulty on validating spinner items and adding value for spinner items我正在使用 android,我发现验证微调器项目和为微调器项目增加价值有困难
【发布时间】:2016-05-23 17:46:59
【问题描述】:

您好,我正在使用 android,我在验证微调器项目和为微调器项目添加价值方面遇到困难,我将向您展示如下代码。

此代码是 UI 部分代码。

<TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/fa_star"
                android:textColor="#97c740"
                android:layout_marginRight="10dp"
                android:id="@+id/choosehotel"
                android:textSize="20dp"
                android:layout_marginTop="13dp"
                android:layout_weight="1" />

            <Spinner
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:hint="Choose your hotel"
                android:layout_marginTop="13dp"
                android:ems="10"
                android:textSize="25dp"
                android:fontFamily="sans-serif"
                android:id="@+id/hotelpreferance"
                android:layout_weight="15"

                android:singleLine="true" />
            <TextView
                android:id="@+id/text0"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

以下是java部分代码

这是使用数组适配器的代码,对代码做了一点改动

public void spinnerdata(){

 hotelpref = (Spinner)findViewById(R.id.hotelpreferance);


   MyClass[] obj2 ={

            new MyClass("No Preference", ""),
            new MyClass("2 star", "2"),
            new MyClass("3 star", "3"),
            new MyClass("4 star", "4"),
            new MyClass("5 star", "5")

    };


    hoteladapter = new SpinAdapter(this ,R.id.spinneritem,obj2 );
    hoteladapter.setDropDownViewResource(R.layout.spinner_items);
    hotelpref.setAdapter(hoteladapter);
    hotelpref.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {


            Toast.makeText(getBaseContext() ,parent.getItemIdAtPosition(position)+"selected", Toast.LENGTH_LONG).show();

        }

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

        }
    });

}

以下是设置值和文本的代码,并为每个文本设置值已更改此代码只是创建了两个名为 MyClass 和 SpinAdapter 的类

class MyClass{

    private String text;
    private String value;


    public MyClass(String text, String value){
        this.text = text;
        this.value = value;
    }

    public void setText(String text){
        this.text = text;
    }

    public String getText(){
        return this.text;
    }

    public void setValue(String value){
        this.value = value;
    }

    public String getValue(){
        return this.value;
    }

     @Override
     public String toString() {
         return this.text;
     }
 }



 class SpinAdapter extends ArrayAdapter<MyClass>{

    // Your sent context
    private Context context;
    // Your custom values for the spinner (User)
    private MyClass[] values;

    public SpinAdapter(Context context, int textViewResourceId,
                       MyClass[] values) {
        super(context, R.layout.spinner_items, textViewResourceId, values);
        this.context = context;
        this.values = values;
    }

    public int getCount(){
        return values.length;
    }

    public MyClass getItem(int position){
        return values[position];
    }

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


    // And the "magic" goes here
    // This is for the "passive" state of the spinner
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // I created a dynamic TextView here, but you can reference your own  custom layout for each spinner item
        TextView label = new TextView(context);
        label.setTextColor(Color.BLACK);
        // Then you can get the current item using the values array (Users array) and the current position
        // You can NOW reference each method you has created in your bean object (User class)
        label.setText(values[position].getText());

        // And finally return your dynamic (or custom) view for each spinner item
        return label;
    }

    // And here is when the "chooser" is popped up
    // Normally is the same view, but you can customize it if you want
    @Override
    public View getDropDownView(int position, View convertView,
                                ViewGroup parent) {
        TextView label = new TextView(context);
        label.setTextColor(Color.BLACK);
        label.setText(values[position].getText());

        return label;
    }

还有一个验证码,这个代码在按钮点击时。

 if(hoteladapter != null){
                String text = ((MyClass)hoteladapter.getItem(hotelpref.getSelectedItemPosition())).getValue();
                if(text.trim().length() == 0){
                    ((TextView)( (LinearLayout)hotelpref.getSelectedView()).findViewById(R.id.spinneritem)).setError("sada");
                }
            }

此代码几乎没有更改,但无法弄清楚主要问题是什么实际上并未在所需位置进行验证。我应该再次向您描述我应该做什么。

我想在单击提交按钮时在此微调器中添加验证,以便将所选项目的第一项设置为“无首选项”,因为它没有值或值“0”,如果它被选中,则应该收到错误消息"请选择一个项目"

【问题讨论】:

    标签: java android validation android-spinner


    【解决方案1】:

    在你的 onItemSelectedListener 中,使用这个:

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        MyClass myClass = (MyClass) parent.getItemAtPosition(position);
    // Validate your spinner values here:
    int value = myClass.getValue();
    String text = myClass.getText();
                    }
    

    我建议您在 MyClass 的数组上使用 ArrayList。比如:

    ArrayList<MyClass> myClassList = new ArrayList<MyClass>();
    

    然后,根据需要创建 MyClass 对象,并将它们添加到数组列表中。

    MyClass myClass = new MyClass("2 star", 1);
    myClassList.add(myClass);
    

    这将是一种更清洁的方法。希望这会有所帮助....

    好的,点击提交按钮:

    btnSubmit.setOnClickListener(new View.OnClickListener() {
    
    
                    @Override
                    public void onClick(View v) {
    
    int pos = spinner.getSelectedItemPosition();
    MyClass myClass = (MyClass)spinner.getAdapter().getItem(pos);
    if(myClass.getValue() == 0){
    // Your toast msg here
    }
    
    
     }
                });
    

    【讨论】:

    • 我必须在按钮单击上验证此微调器,例如酒店偏好如果“无偏好”,因为它的值为 0,则消息应显示为“您尚未选择任何项目”所以我必须为此做些什么
    • 好的,使用下面的代码: if(spinner.getAdapter != null){ String day = spinner.getSelectedItem().toString().trim();} getSelectedItem() 返回一个对象,因此您可以使用它从 MyClass 类中获取值。
    • 接下来我要做的是验证这个微调器项目,如果选择“无偏好”作为酒店偏好中的示例,我无法理解如何执行它值为 0 时应显示错误消息“请选择一个项目”,所以我需要知道执行此过程的步骤是什么
    • 里面的对象日是什么
    • @moulickkarmakar,该对象将是 MyClass 类型的对象:您可以从中转换 MyClass,如下所示: MyClass myclass= (MyClass) parent.getItemAtPosition(position);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    相关资源
    最近更新 更多