vendredi 21 août 2015

Android checkbox and spinner

i am new to android, i am trying to post chechbox and spinner values into i mysql database, i am already posting text box values without a problem, but when i have to combine textboxes with chech boxes then am stuck, i already have the following code, that works without checkboxes.

 <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://ift.tt/nIICcg"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"

   android:padding="5dip">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_weight="1" 
    android:layout_gravity="center">

<EditText
    android:id="@+id/inputAgentid"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textEmailAddress" 
    android:background="#ffffff"
    android:layout_marginBottom="3dip"
    android:hint="Agent Id"
    android:textColorHint="#999999"
    android:padding="10dp">

    <requestFocus />
</EditText>



<EditText
    android:id="@+id/inputQuantity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textEmailAddress" 
    android:background="#ffffff"
    android:layout_marginBottom="3dip"
    android:hint="Quantity"
    android:textColorHint="#999999"
    android:padding="10dp">

    <requestFocus />
</EditText>

<Button
        android:id="@+id/btnCreateProduct"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Place Order" 
        android:textColor="#000000"
        android:background="#ffffff"
        android:layout_marginTop="10dip"/>

the following is the java:

JSONParser jsonParser = new JSONParser();
EditText inputAgentid;
EditText inputQuantity;
Button btnCreateProduct;

// url to create new product
private static String url_create_product = "http://ift.tt/1NA0Tsk";

// JSON Node names
private static final String TAG_SUCCESS = "success";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_order_page);

    // Edit Text
    inputAgentid = (EditText) findViewById(R.id.inputAgentid);
    inputQuantity = (EditText) findViewById(R.id.inputQuantity);

    // Create button
    Button btnCreateProduct = (Button) findViewById(R.id.btnCreateProduct);

    // button click event
    btnCreateProduct.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // creating new product in background thread
            new CreateNewProduct().execute();
        }
    });
}

/**
 * Background Async Task to Create new product
 * */
class CreateNewProduct extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(OrderPage.this);
        pDialog.setMessage("Creating Product..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    /**
     * Creating product
     * */
    protected String doInBackground(String... args) {
        String agentid = inputAgentid.getText().toString();
        String quantity = inputQuantity.getText().toString();

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("agentid", agentid));
        params.add(new BasicNameValuePair("quantity", quantity));

        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                "POST", params);

        // check log cat fro response
        Log.d("Create Response", json.toString());

        // check for success tag
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // successfully created product
                Intent i = new Intent(getApplicationContext(),    MainActivity.class);
                startActivity(i);

                // closing this screen
                finish();
            } else {
                // failed to create product
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once done
        pDialog.dismiss();
      }

    }
}

i need to add the the following checkboxes into the xml code:

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="cellphone" />

<CheckBox
    android:id="@+id/checkBox2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="laptop" />

can someone please show me how am i can fit the checkboxes into my java so that i can post their values into a mysql db?




Aucun commentaire:

Enregistrer un commentaire