如何整数值分配给基于项变量对Android微调选择? [英] How to assign Integer Value to variable based on ITEM selected on the Android Spinner.?
问题描述
我已经设置了数组适配器为我的微调来显示成绩的下拉列表(A +,A,...等)。
所有我需要的是当用户选择,A +,对myApp应该把它理解为浮点值10,从而使A +(= 10)的值可以在一级方程式需要整数计算总成绩中。
对于例如,如果用户选择B +,我的应用程序应该理解成8.5,可在公式中使用。
I have set a Array Adapter for my Spinner to display a drop-down list of Grades (A+, A, ...etc). All I need is when the user selects, A+, myApp should understand it as float value 10, so that the Value of A+ (=10) can be used in Formula that needs integer to calculate the overall grade. for eg, if user select B+, my APP should understand as 8.5, which can be used in the formula.
基本上,我希望用户看到在纺纱的成绩,但我需要它的实际值进行计算。
Basically, I want the USERS to see the Grades on the spinners but I need the Actual values of it for Calculation.
我可以利用的纱厂项目的位置吗?
请显示一个例子,如何找回它。
Can I make use of POSITION of Items on the Spinners? Kindly show an example how to retrieve it.
我的字符串资源:
<?xml version="1.0" encoding="utf-8"?>
<resources>
</string-array>
<string-array name="grade_list">
<item>A+</item>
<item>A</item>
<item>A-</item>
<item>B+</item>
<item>B</item>
<item>B-</item>
<item>C+</item>
<item>C</item>
<item>C-</item>
<item>D</item>
<item>E</item>
<item>U</item>
</resources>
我的Java code
MY JAVA CODE
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class CSEsem1 extends Activity implements OnClickListener{
Spinner cs1spin1;
Spinner cs1spin2;
Spinner cs1spin3;
Spinner cs1spin4;
Spinner cs1spin5;
Spinner cs1spin6;
Spinner cs1spin7;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.laycsesem1);
ArrayAdapter<CharSequence> grade = ArrayAdapter.createFromResource(this,
R.array.grade_list, android.R.layout.simple_spinner_item);
grade.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cs1spin1.setAdapter(sem_adapter);
cs1spin1.setOnItemSelectedListener(this);
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// Completely blank after this.
}
我需要申请相同的所有纺纱厂。我有7个总在我的XML文件中。
I need to apply the same for all the spinners. I have 7 in total in my XML file.
这是我想到的是,由于我对纱厂项目可以被转换成字符串,我可以用一个开关(字符串),但Android是Java的1.5,1.6 ..和开关(串)下编译的方法是刊登在的Java 1.7。所以不能用这个。
A method which I thought of was, Since my ITEMS on the Spinners can be converted to String, I could have used a switch (String) but Android is compiled under Java 1.5,1.6 ..and switch(String) is a featured in Java 1.7. So cannot use this.
推荐答案
我建议你创建包含对象,如
I suggest you to create a custom adapter that holds objects like
public class Grade{
String mLabel;
Float mValue;
.......
}
看那个<一个href=\"http://stackoverflow.com/questions/6497871/create-adapter-to-fill-spinner-with-objects\">answer这<一个href=\"http://androidexample.com/Spinner_Basics_-_Android_Example/index.php?view=article_discription&aid=82&aaid=105\"相对=nofollow>例如。
所以定义对象的名单中的一员你的 Acticity
/ 片段
。
之后,在 onItemSelected
带你通过一个给定的位置对象。
Look at that answer and this example.
So define that list of objects as a member of your Acticity
/ Fragment
.
After that in onItemSelected
take you object by a given position.
这篇关于如何整数值分配给基于项变量对Android微调选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!