首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >android :忽略微调器的条件数组适配器

android :忽略微调器的条件数组适配器
EN

Stack Overflow用户
提问于 2012-05-23 17:02:40
回答 1查看 265关注 0票数 0

我是Android的新手。我试图为不同的字符串数组形成一个条件适配器(依赖于一个字符串变量)。

代码语言:javascript
运行
复制
    TextView textPrompt;
    textPrompt = (TextView)findViewById(R.id.textprompt);
    final String acType = i.getStringExtra("type");
    textPrompt.setText(acType);
    if (acType == "400G"){
    spinnerSurface = (Spinner) findViewById(R.id.spinnerSurface);
    ArrayAdapter<CharSequence> adapterSurface = ArrayAdapter.createFromResource(
            this, R.array.surface_option_1, android.R.layout.simple_spinner_item);
    adapterSurface.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerSurface.setAdapter(adapterSurface);

    }
    else if (acType != "400G"){
        spinnerSurface = (Spinner) findViewById(R.id.spinnerSurface);
        ArrayAdapter<CharSequence> adapterSurface = ArrayAdapter.createFromResource(
                this, R.array.surface_option, android.R.layout.simple_spinner_item);
        adapterSurface.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerSurface.setAdapter(adapterSurface);

    }

    spinnerSurface.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() { 
        public void onItemSelected(AdapterView<?> parent, View v, 
                int position, long id) { 
            TextView tx = (TextView)v; 
            Log.i("\n\nid",String.valueOf(tx.getText()));
        } 
        public void onNothingSelected(AdapterView<?> arg0) { 
            // TODO Auto-generated method stub 
        } 
    });

我使用textPrompt检查acType的值。无论acType是"400G“还是不是"400G",程序都会将acType作为非"400G”,因此采用R.array.surface_option而不是R.array.surface_option1。请帮帮忙。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-23 17:15:10

这实际上是一个Java问题,而不是android -您不应该使用==来比较String,而应该使用equals()

代码语言:javascript
运行
复制
if (acType != null && acType.equals("400G")){
    ...
}
else {
    ...
}

因为String是对象,所以==比较引用,对于相等的字符串,引用很可能是不同的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10716756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档