Load Data setOnItemSelectedListener

China☆狼群 提交于 2019-12-20 06:28:19

问题


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:


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...


来源:https://stackoverflow.com/questions/16460101/load-data-setonitemselectedlistener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!