简体   繁体   中英

get value from checkbox and show in other activity

I need get te value from checkbox and show in other activity and display in textview.

This is the code FormSupervisar.

public class FormSupervisar extends Activity {  
    CheckBox Si;      
    @Override       
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);            
        setContentView(R.layout.formsupervisar);           
        Si=(CheckBox)findViewById(R.id.chBoxSi);  
    }
    public void btnEnviar(View view){
        if (Si.isChecked()){        
            Intent i = new Intent(this,FormBotonSi.class);
            i.putExtra("Si",Si.getText().toString().trim());     
            startActivity(i);  
        }      
    }

The activity when i call the string and show the String in a TextView

public class FormBotonSi extends Activity {   
    private String ,Si ;     
    private TextView tvTipoInspeccion;                     
    @Override      
    public void onCreate(Bundle savedInstanceState)      
    {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.formbotonsi);                                           
         Bundle bn1 = getIntent().getExtras();  
         Si = bn1.getString(Si);          
         tvTipoInspeccion.setText(Si.toString());
    }
}

use

Si = bn1.getString("Si");

instead of

Si = bn1.getString(Si);

for getting value in second activity because you are setting "Si" as key in first activity(in FormSupervisar)

OR You can initialize Si String as:

private String Si="Si";    
Si = bn1.getString(Si);

Try it this way.....

public class FormBotonSi extends Activity {

......
......

String Si = getIntent().getExtras().getString("Si");  // its "Si" instead of Si


}

One more thing... While you code in Java you must follow the Camel case. Field and Methods always starts with small letter whereas Class, Interface, Enums, Constructors starts with Capital letters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM