0

我是android的新手,我试图通过Intent函数接收一个变量,并且根据变量的值应该显示一个contentview。

Bundle parametros = getIntent().getExtras();
String type = parametros.getString("tipo");
int accao = parametros.getInt("accao");

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (type=="medicamentos") {
        Button voltar = (Button) findViewById(R.id.button1);
        voltar.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent medIntent = new Intent(view.getContext(), Listar.class);
                startActivityForResult(medIntent, 0);
            }
        });
        if (accao==1)
            setContentView(R.layout.adicionar_medicamentos);

        if (accao==2)
            setContentView(R.layout.editar_medicamentos);
    }
}

我做错了什么?谢谢

4

2 回答 2

0

用于equals()比较字符串。

==比较对象引用而不是它们的内容。

if(type.equals("medicamentos")) {
  ....
}
于 2013-03-12T17:02:30.317 回答
0
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

String type = getIntent().getString("tipo");
int accao = getIntent().getInt("accao");

if (type != null && type.equals("medicamentos")) {
    Button voltar = (Button) findViewById(R.id.button1);
    voltar.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent medIntent = new Intent(view.getContext(), Listar.class);
            startActivityForResult(medIntent, 0);
        }
    });
    if (accao==1)
        setContentView(R.layout.adicionar_medicamentos);

    if (accao==2)
        setContentView(R.layout.editar_medicamentos);
}

}

于 2013-03-12T17:11:40.633 回答