Load Data setOnItemSelectedListener

2019-03-04 03:57发布

Problem: I load my class, the setOnItemSelectedListener function runs and sets variable wattage to a specific value, depending on what item was selected in the spinner.

How can I prevent this from happening, how can I include a custom value for a variable wattage?

Detail:

I have a class called edit lighting. In this class all I want to do is load the data from the database. Currently when the light type spinner is selected, I associate a value with it. e.g. I pick CFL - wattage is then 26.

But I let the user add a custom wattage in an edit text field.

My problem is, when loading the data in edit lighting (the class below) the setOnItemSelectedListener function runs straight away. How can I prevent this?

How can I prevent this function from triggering straight away, the custom wattage is shown?

Can anyone offer a viable solution?

Thanks in advance. See code below

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();
            }

        });


    }
}

1条回答
forever°为你锁心
2楼-- · 2019-03-04 04:22

Use the code below:

// 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...
查看更多
登录 后发表回答