[英]Writing events for dynamically generated checkbox in a table
I have a page where there is a dynamically generated table with the last column which has checked checkboxes.The final bill is shown below the table. 我有一个页面,其中有一个动态生成的表格,其中最后一列已选中复选框。最终账单显示在表格下方。 As i uncheck the checkboxes the amount should decrease in the final table.So i have to know which corresponding checkbox was clicked in the same row as the amount. 当我取消选中复选框时,金额应在最终表中减少。因此,我必须知道在与金额相同的行中单击了哪个对应的复选框。
package com.example.shopkart;
import java.sql.SQLException;
import java.util.ArrayList;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class checkout extends Activity {
datamanager dm;
String name,mailid;
int bill=0;
TableLayout tbcheckout;
TextView txtbill;
int[] amount_product;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checkout);
dm=new datamanager(this);
tbcheckout=(TableLayout) findViewById(R.id.tbcheckout);
txtbill=(TextView) findViewById(R.id.txtbill);
name=getIntent().getExtras().getString("name");
mailid=getIntent().getExtras().getString("mailid");
try {
viewtable();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void viewtable() throws SQLException
{
ArrayList<ArrayList<Object>> data=dm.getAllRowsAsArrays1(mailid);
amount_product=new int[data.size()];
TableRow tbrow0 = new TableRow(this);
TextView tv0 = new TextView(this);
tv0.setText(" Product Name ");
tv0.setTextColor(Color.parseColor("#4B0082"));
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setText(" Product Price ");
tv1.setTextColor(Color.parseColor("#4B0082"));
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(" Pay Now ");
tv2.setTextColor(Color.parseColor("#4B0082"));
tbrow0.addView(tv2);
tbcheckout.addView(tbrow0);
for (int position=0; position < data.size(); position++)
{
TableRow tbrow = new TableRow(this);
ArrayList<Object> row = data.get(position);
TextView t1v = new TextView(this);
t1v.setText(row.get(0).toString());
t1v.setTextColor(Color.parseColor("#4B0082"));
t1v.setGravity(Gravity.CENTER);
tbrow.addView(t1v);
TextView t2v = new TextView(this);
t2v.setText(row.get(1).toString());
t2v.setTextColor(Color.parseColor("#4B0082"));
t2v.setGravity(Gravity.CENTER);
tbrow.addView(t2v);
amount_product[position]=Integer.parseInt(row.get(1).toString());
bill+=Integer.parseInt(row.get(1).toString());
CheckBox cb1=new CheckBox(this);
cb1.setText("Include");
cb1.setTextColor(Color.parseColor("#4B0082"));
cb1.setChecked(true);
tbrow.addView(cb1);
tbcheckout.addView(tbrow);
}
txtbill.setText("Your final cart amount is Rs "+bill);
}
}
I placed the amounts of each item in an array so that i can refer them later.How do i refer to the corresponding checkbox and write event for them? 我将每个项目的金额放在一个数组中,以便以后可以引用它们。如何引用相应的复选框并为它们编写事件?
1.You should set the setonCheckedChangeListener()
,like this 1.您应该像这样设置setonCheckedChangeListener()
cb1.setOnCheckedChangeListener(this);
In your Activity checkout
在您的活动checkout
public class checkout extends Activity implements OnCheckChangedListener
Implement here 在这里实施
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
Object tag=buttonView.getTag();
//could be integer, or primitive datatype
//just make sure of type-casting
if ( isChecked )
{
// perform logic, reduce value in final table or such
}
}
2.Next set tag to checkbox, for more reference see here setTag(Object) 2.接下来将标签设置为复选框,有关更多参考,请参见此处setTag(Object)
cb1.setTag(anyObject);
which can be used to get some value on object to perform any operation ,which in this case is to reduce value in final table. 可以用来获取对象上执行任何操作的值,在这种情况下,这是减少最终表中的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.