0

问题:我加载了我的类,setOnItemSelectedListener 函数运行并将变量瓦数设置为特定值,具体取决于在微调器中选择的项目。

如何防止这种情况发生,如何为可变功率包含自定义值?

细节:

我有一个名为编辑照明的课程。在这个类中,我要做的就是从数据库中加载数据。Currently when the light type spinner is selected, I associate a value with it. 例如,我选择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();
            }

        });


    }
}
4

1 回答 1

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...
于 2013-05-09T12:32:52.297 回答