你好,我正在研究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");
}
}
在此代码中已经做了很少的更改但是无法弄清楚主要问题是什么并没有实际在所需位置进行验证。我应该再次描述一下我的想法。
我想在提交按钮单击时在此微调器中添加验证,以便将所选项目形成为第一个项目&#34;没有偏好&#34;因为它没有价值或价值&#34; 0&#34;如果选中,则应收到错误消息为&#34;请选择一个项目&#34;
答案 0 :(得分:0)
在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
}
}
});