加载数据setOnItemSelectedListener [英] Load Data setOnItemSelectedListener

查看:435
本文介绍了加载数据setOnItemSelectedListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

问题:我载入我的课上,setOnItemSelectedListener函数运行   并设置变量瓦数为特定的值,这取决于产品   在微调被选中。

     

我怎样才能prevent这种情况发生,我怎么能包括自定义   的变量值功率?

说明:

我有一类叫做编辑照明。在这一类中的所有我想要做的是从数据库加载数据。当前在选择了光型喷丝,我联想到它的值。例如我挑的 CFL - 功率然后26

不过,我让用户在编辑文本字段中添加自定义的功率。

我的问题是,在编辑照明加载时的数据(以下级)在 setOnItemSelectedListener 函数运行立竿见影。我怎样才能prevent呢?

我怎样才能prevent从触发直线距离该功能,自定义功率显示?

任何人都可以提供一个可行的解决方案?

在此先感谢。 见下文code

 公共类EditLighting延伸活动{

    公共微调mEdit;
    公共mEdit3;
    公共字符串lightingType,瓦数;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.editlighting);

        如果(价值==检索){
            SAMPLEDB = getBaseContext()openOrCreateDatabase(ClientList.retrievedClient +,MODE_PRIVATE,空分贝)。
        }

         光标C = sampleDB.rawQuery(SELECT * FROM tbl_lightingData WHERE l_id =?,新的String [] {RoomOverview.getID [1]});

         如果(C!= NULL){
            如果(c.moveToFirst()){
                做 {
                    //获取光源类型
                    lightingType = c.getString(c.getColumnIndex(lightType));

                    如果(lightingType.equals(CFL)){
                        lightIndex = 0;
                    }
                    否则,如果(lightingType.equals(节能灯相当于100W卤素灯斑)){
                        lightIndex = 1;
                    }

                    mEdit =(微调)findViewById(R.id.t1);
                    mEdit.setSelection(lightIndex);
                    //mEdit.setEnabled(false);

                    瓦数= c.getString(c.getColumnIndex(lightWattage));
                    mEdit3 =(EditText上)findViewById(R.id.t4);
                    mEdit3.setText(瓦特);
                    Toast.makeText(getApplicationContext(),瓦数=+瓦数,Toast.LENGTH_LONG).show();



                }而(c.moveToNext());
            }
        }
        c.close();

        mEdit.setOnItemSelectedListener(新OnItemSelectedListener(){
            公共无效onItemSelected(适配器视图<>为arg0,查看ARG1,INT ARG2,长ARG3){

                。lightingType = mEdit.getSelectedItem()的toString();

                如果(lightingType.equals(CFL)){
                    瓦数=12;
                    mEdit3.setText(瓦特);
                }
                否则,如果(lightingType.equals(节能灯相当于100W卤素灯斑)){
                    瓦数=18;
                    mEdit3.setText(瓦特);
                }

                Toast.makeText(getApplicationContext(),WATTAGE2 =+瓦数,Toast.LENGTH_LONG).show();
            }

        });


    }
}
 

解决方案

使用code如下:

  //如果为假就意味着我们正在处理的初始选择,所以我们应该忽略它
私人布尔MFLAG = FALSE;

mEdit.setOnItemSelectedListener(新OnItemSelectedListener(){
    公共无效onItemSelected(适配器视图<>为arg0,查看ARG1,INT ARG2,长ARG3){
         如果(!MFLAG){
             //从这一刻开始选择将被接受
             MFLAG = TRUE;
             返回;
         }
         // ...的code休息...
 

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

        });


    }
}

解决方案

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

这篇关于加载数据setOnItemSelectedListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆