获取该行中的按钮单击行的值

时间:2013-06-19 11:10:42

标签: android button tablerow

我的Android应用程序正在填充从sqlite数据库以编程方式创建的表格布局。表格布局的最后一列有两个按钮。这两个按钮通过线性布局分配给一列。

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    35, 35);

            params.setMargins(10, 0, 10, 0);
            seventhCol1=new Button(this);
            seventhCol1.setText("A");
            seventhCol1.setLayoutParams(params);
            //seventhCol1.setLayoutParams(new LayoutParams(30, 30));
            seventhCol1.setId(71);
            seventhCol1.setOnClickListener(mListener);

            seventhCol2=new Button(this);
            seventhCol2.setText("C");
            seventhCol2.setLayoutParams(params);
            seventhCol2.setId(72);
            seventhCol2.setOnClickListener(nListener);

            btn_linear=new LinearLayout(this);
            btn_linear.setOrientation(LinearLayout.HORIZONTAL);
            btn_linear.addView(seventhCol1);
            btn_linear.addView(seventhCol2);
            btn_linear.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
            btn_linear.setGravity(Gravity.CENTER);
            tr.addView(btn_linear);

单击按钮seventhCol1我想从单击该按钮的特定行获取数据...我该怎么做? 我试过这种方法:

tr.setOnClickListener(rowclickListner);

private OnClickListener rowclickListner=new OnClickListener() {

    @Override
    public void onClick(View v) {


// TODO Auto-generated method stub
    TableLayout tl=(TableLayout) v.getParent();
    /*TableRow tr=(TableRow) tl.getParent();*/
    TableRow tr=(TableRow) tl.getParent();
    TextView tv_mobile=(TextView)tr.getChildAt(2);
    String mobile=tv_mobile.getText().toString();

    Toast.makeText(LeadManagement.this, mobile, Toast.LENGTH_LONG).show();
    /*ArrayList<String> leaddetails=mDbHelper.getTableDetails("lead_table", mobile);
     prefEditor.putBoolean("leadbundle", true);
     prefEditor.commit();
     startActivity(new Intent(LeadManagement.this,CreateLead.class).putExtra("leadlist", leaddetails));*/
    }
};

但它显示错误!!我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

不要将onClickListener注册为类成员,而应注册为匿名对象。类似的东西:

Button tr = // your button corresponding to N-th row
        final RowData rowData = ... // get the data from row N
        tr.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                someMethodCall(rowData)
            }
        });