0

我的 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));*/
    }
};

但它显示错误!我该怎么做?

4

1 回答 1

0

不要将 an 注册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)
            }
        });
于 2013-06-19T11:21:35.140 回答