my xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="Select Products"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/buy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="92dp"
android:text="Pay" />
<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/cb2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"
android:text="Casual Shirt - 400 Rs" />
<CheckBox
android:id="@+id/cb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/buy"
android:layout_alignLeft="@+id/cb3"
android:layout_marginBottom="34dp"
android:text="t-Shirt - 200 Rs" />
<CheckBox
android:id="@+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/cb2"
android:layout_centerVertical="true"
android:text="Shorts - 300 Rs" />
<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/cb3"
android:layout_alignLeft="@+id/cb1"
android:layout_marginBottom="17dp"
android:text="Jeans - 500 Rs" />
</RelativeLayout>
java file
package com.example.multiactivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
public class MaleActivity extends Activity {
Button b1;
CheckBox cb1, cb2, cb3, cb4;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.male);
b1 = (Button) findViewById(R.id.buy);
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
cb4 = (CheckBox) findViewById(R.id.cb4);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String added = "";
if (cb1.isChecked()) {
int a = 400;
}
if (cb2.isChecked()) {
int b = 500;
}
if (cb3.isChecked()) {
int c = 300;
}
if (cb4.isChecked()) {
int d = 200;
} else {
return;
}
}
});
}
}
I am trying to add all selected checkboxes.
I am trying to get the total of all added products.
I am unable to select all checkedboxes for the total figure.
I want to add the selected int and get the total
ex. int a + b + c;
Help
Thanks.
It looks like you're saying if a box is checked, then set an int
to a value.
If your values are static, just initalize them ahead of time and do something like
int sum = 0;
if (cb1.isChecked()) {
sum += 400;
}
edit: if your clickability is a problem refer to https://developer.android.com/reference/android/view/View.html#attr_android%3aclickable
use this java file (please, note the comment at the end of the "onClick" function)
package com.example.multiactivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
public class MaleActivity extends Activity {
Button b1;
CheckBox cb1, cb2, cb3, cb4;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.male);
b1 = (Button) findViewById(R.id.buy);
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
cb4 = (CheckBox) findViewById(R.id.cb4);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String added = "";
int sum = 0;
if (cb1.isChecked()) {
sum += 400;
}
if (cb2.isChecked()) {
sum += 500;
}
if (cb3.isChecked()) {
sum += 300;
}
if (cb4.isChecked()) {
sum += 200;
}
//now in "sum" you have the sum
}
});
}
}
I would suggest creating a private field called total
private int total;
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String added = "";
int a = 0;
int b = 0;
int c = 0;
int d = 0;
if (cb1.isChecked()) {
a = 400;
}
if (cb2.isChecked()) {
b = 500;
}
if (cb3.isChecked()) {
c = 300;
}
if (cb4.isChecked()) {
d = 200;
}
total = a + b + c + d; // or total += a + b + c + d;
}
}
and then you can use total wherever you want
Hope this helps :)
I guess your onClick()
method could be this instead
String added;
@Override
public void onClick(View v) {
int a = 0;
if (cb1.isChecked()) a+=400;
if (cb2.isChecked()) a+=500;
if (cb3.isChecked()) a+=300;
if (cb4.isChecked()) a+=200;
added = Integer.toString(a);
}
I don't really know why the checkboxes wouldn't work though.
I think you had better replace your code with this
HashMap<String,Boolean> items = new HashMap<>();
checkbox1.setOnCheckedChangeListener(new CompoundButton.onCheckedChangeListener(){
@Override
public void onCheckedChange(CompundButton buttonView,boolean isChecked){
if(isChecked){items.put("order982929",true);}
}
});
Continue with other checkboxes and finally convert this HashMap to json and send it to your server
After clicking pay button App stops.
// MaleActivity.java
package com.example.multiactivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
public class MaleActivity extends Activity {
Button b1;
CheckBox cb1, cb2, cb3, cb4;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.male);
b1 = (Button) findViewById(R.id.buy);
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
cb4 = (CheckBox) findViewById(R.id.cb4);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int sum = 0;
if (cb1.isChecked()) {
sum += 400;
}
if (cb2.isChecked()) {
sum += 500;
}
if (cb3.isChecked()) {
sum += 300;
}
if (cb4.isChecked()) {
sum += 200;
}
int a = sum;
Intent i = new Intent(MaleActivity.this, LastPage.class);
i.putExtra("total", a);
startActivity(i);
}
});
}
}
My lastpage activity.
//LastPage.java
package com.example.multiactivity;
import org.w3c.dom.Text;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class LastPage extends Activity {
TextView t2;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.last_page);
t2 = (TextView) findViewById(R.id.textView2);
Intent i= getIntent();
Bundle b = i.getExtras();
if(b!=null)
{
String j =(String) b.get("total");
t2.setText("Your Total is "+ j);
}
}
}
App stops.
Help
thanks
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.