问题:我加载了我的类,setOnItemSelectedListener函数运行 并将可变瓦数设置为特定值,具体取决于项目 在旋转器中被选中。
如何防止这种情况发生,如何添加自定义 可变瓦数的值?
详情:
我有一个名为编辑照明的课程。在这个类中,我想要做的就是从数据库加载数据。目前,当选择灯光类型微调器时,我将一个值与它相关联。例如我选择 CFL - 瓦数然后是26.
但我让用户在编辑文本字段中添加自定义功能。
我的问题是,当在编辑灯光(下面的类)中加载数据时, setOnItemSelectedListener 函数会立即运行。我该如何防止这种情况?
如何防止此功能立即触发,显示自定义功率?
有人能提供可行的解决方案吗?
提前致谢。 请参阅下面的代码
public class EditLighting extends Activity {
public Spinner mEdit;
public mEdit3;
public String lightingType, wattage;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editlighting);
if(value == "Retrieve"){
sampleDB = getBaseContext().openOrCreateDatabase(ClientList.retrievedClient+".db", MODE_PRIVATE, null);
}
Cursor c = sampleDB.rawQuery("SELECT * FROM tbl_lightingData WHERE l_id =?", new String[] {RoomOverview.getID[1]});
if (c != null ) {
if (c.moveToFirst()) {
do {
//get light type
lightingType = c.getString(c.getColumnIndex("lightType"));
if(lightingType.equals("CFL")){
lightIndex = 0;
}
else if (lightingType.equals("CFL Equivalent Halogen 100w spot")){
lightIndex = 1;
}
mEdit = (Spinner)findViewById(R.id.t1);
mEdit.setSelection(lightIndex);
//mEdit.setEnabled(false);
wattage = c.getString(c.getColumnIndex("lightWattage"));
mEdit3 = (EditText)findViewById(R.id.t4);
mEdit3.setText(wattage);
Toast.makeText(getApplicationContext(), "WATTAGE = "+wattage, Toast.LENGTH_LONG).show();
}while (c.moveToNext());
}
}
c.close();
mEdit.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
lightingType = mEdit.getSelectedItem().toString();
if(lightingType.equals("CFL")){
wattage = "12";
mEdit3.setText(wattage);
}
else if (lightingType.equals("CFL Equivalent Halogen 100w spot")){
wattage = "18";
mEdit3.setText(wattage);
}
Toast.makeText(getApplicationContext(), "WATTAGE2 = "+wattage, Toast.LENGTH_LONG).show();
}
});
}
}
答案 0 :(得分:5)
使用以下代码:
// if false it means we're dealing with the initial selection so we should ignore it
private boolean mFlag = false;
mEdit.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if (!mFlag) {
// from this moment on the selection will be accepted
mFlag = true;
return;
}
// ...rest of code...