Changing content of a Spinner using a radio button

0

I used this code to change the contents of a Spinner with a radiobutton but it does not work. I ask for your help:

public class ExemploActivity extends Activity {
    ImageView image;
    EditText nome,codigo;
    Spinner sexo, curso;
    RadioButton tecnologias, letras;
    RadioGroup r1;
    private static final String[] sex = {"","Masculino","Feminino"};
    private static final String[] AFCT = {"","Energias Renovaveis","TIC's"};
    private static final String[] AFAG = {"","Direito","Recursos Humanos"};
    ArrayAdapter<String> operation,genero,tecs,Camera;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.exemplo);

        if(tecnologias.isSelected()){
            operation = new ArrayAdapter<String>(ExemploActivity.this,android.R.layout.simple_spinner_item,AFCT);

        }else if (letras.isSelected()){
            operation = new ArrayAdapter<String>(ExemploActivity.this,android.R.layout.simple_spinner_item,AFAG);

        }

        curso.setAdapter(tecs);

        genero = new    ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,sex);
        nome = (EditText)findViewById(R.id.editText1);
        codigo = (EditText)findViewById(R.id.editText2);

        sexo=( Spinner)findViewById(R.id.spinner1);
        sexo.setAdapter(genero);

        tecnologias=(RadioButton)findViewById(R.id.radio1);
        letras=(RadioButton)findViewById(R.id.radio2);

        image = (ImageView)findViewById(R.id.imageView1);
        Camera = new    ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,sex);
        /*image.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 0);
            }
        });*/

        image.setOnLongClickListener(new OnLongClickListener() {    
            @Override
            public boolean onLongClick(View v) {
                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 0);
                return true;
            }
        });

        curso=( Spinner)findViewById(R.id.spinner2);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==0){
            Bitmap theImage = (Bitmap)data.getExtras().get("data");
            image.setImageBitmap(theImage);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.exemplo, menu);
        return true;
    }
}
    
asked by anonymous 16.06.2014 / 13:53

1 answer

1

You should be getting a NullPointerExeception right?

You are only associating the "technologies" attribute after trying to use an internal method of it, your code should look like this to be correct:

tecnologias = (RadioButton) findViewById(R.id.radio1);
    letras = (RadioButton) findViewById(R.id.radio2);

    tecnologias.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            operation = new ArrayAdapter<String>(ExemploActivity.this, android.R.layout.simple_spinner_item, AFCT);
            operation.notifyDataSetChanged();
        }
    });

You have to add listeners to know when your RadioButtons are triggered by the user. It is based on this and one that you study ...

Att

    
16.06.2014 / 16:01