在表中为动态生成的复选框编写事件

时间:2014-04-25 12:53:06

标签: java android eclipse

我有一个页面,其中有一个动态生成的表,最后一列检查了复选框。最终的帐单显示在表格下方。当我取消选中复选框时,金额应该在最终表格中减少。所以我必须知道在金额的同一行中点击了哪个相应的复选框。

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);

        }


}

我将每个项目的金额放在一个数组中,以便稍后可以引用它们。我如何参考相应的复选框并为它们编写事件?

1 个答案:

答案 0 :(得分:1)

1.您应该设置setonCheckedChangeListener(),就像这样

cb1.setOnCheckedChangeListener(this);

在您的活动checkout

public class checkout extends Activity implements OnCheckChangedListener

在此实施

  @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.接下来将标签设置为复选框,有关详细信息,请参阅此处setTag(Object)

   cb1.setTag(anyObject);

可用于获取对象的某些值以执行任何操作,在这种情况下是减少最终表中的值。