android如何通过单选按钮存储数据?

时间:2015-01-28 16:44:05

标签: java android radio-button

如何在json中存储2个选项?

点击btnsubmit会运行我选择的选择选项吗?

btnSubmit.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

if (onRadioButtonClicked.selected()){
   //run option1
  UserFunctions userFunction = new UserFunctions();//call the function from userfunctions.java
  JSONObject json = userFunction.storePurhcase(pid, time, email, name.  branch);//pass the value to php
} else {
   //run option2
  UserFunctions userFunction = new UserFunctions();//call the function from userfunctions.java
  JSONObject json = userFunction.storePurhcase(pid, time, email, name, address, comment);//pass the value to php
  }
}
}

选项1 ,如果选择RadioButtonpickup只需pid, time, email, name, branch而不需要address and comment

选项2 ,如果选择RadioButtondelivery,则选择仅pid, time, email, name, address, comment存储branch

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

    //get data from database
    DatabaseHandler db = new DatabaseHandler(getApplicationContext());
    String email = db.getEmail();
    String address = db.getAddress();
    String name = db.getName();
    String time = db.gettime();


    // Button Click event
    btnsubmit.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            String address = inputetaddress.getText().toString();
            Intent intent = getIntent();
            String pid = intent.getStringExtra("productid");
            String time = inputitemtime.getText().toString();
            String comment =inputetcomment.getText().toString();
            String email =  inputitememail.getText().toString();
            String name =  inputitemusername.getText().toString();
            String branch = dropdownbox;

            UserFunctions userFunction = new UserFunctions();//call the function from userfunctions.java
            JSONObject json = userFunction.storePurhcase(pid, time, email, name, address, comment,  branch);//pass the value to php

        }
    });

}


public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
    case R.id.radioButtonpickup:
        if (checked)

            inputcomment.setVisibility(View.GONE);  
            inputetcomment.setVisibility(View.GONE);    
            inputaddress.setVisibility(View.GONE);  
            inputetaddress.setVisibility(View.GONE);

            spinner = (Spinner) findViewById(R.id.spinner);
            spinner.setOnItemSelectedListener(this);
            List<String> pickupaddress = new ArrayList<String>();
            pickupaddress.add("A Plaza");
            pickupaddress.add("B Plaza");
            pickupaddress.add("C  Plaza");
            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, pickupaddress);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(dataAdapter); 
        break;

    case R.id.radioButtondelivery:
        if (checked)

            spinner.setVisibility(View.GONE);   
            inputitemcomment.setVisibility(View.VISIBLE);   
            inputetcomment.setVisibility(View.VISIBLE); 
            inputitemaddress.setVisibility(View.VISIBLE);   
            inputetaddress.setVisibility(View.VISIBLE);

        break;
    }
}



    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
        // On selecting a spinner item
        String item = parent.getItemAtPosition(position).toString();
        dropdownbox = item;

        // Showing selected spinner item
        Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_SHORT).show();
    }

    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
    }

0 个答案:

没有答案