Java onClickListener事件未在TableLayout和动态n行上触发

Java onClickListener事件未在TableLayout和动态n行上触发,java,android,onclicklistener,android-tablelayout,Java,Android,Onclicklistener,Android Tablelayout,我一直试图在选择表行时触发onClickListener事件 在xml中定义的表布局中动态添加表行(从数组中获取)(xml中只定义了一个空表布局)。 更新: 在循环中移动OnClickListener(单选按钮)可以工作..但不能在“tablerow”上 有人能指出我的错误并纠正我吗? 如果有人能为我正在尝试做的事情发布同上的示例,我会更加感激。 谢谢 以下是填充表格布局的代码: protected void onCreate(Bundle savedInstanceState) {

我一直试图在选择表行时触发onClickListener事件

在xml中定义的表布局中动态添加表行(从数组中获取)(xml中只定义了一个空表布局)。

更新:
在循环中移动OnClickListener(单选按钮)可以工作..但不能在“tablerow”上

有人能指出我的错误并纠正我吗? 如果有人能为我正在尝试做的事情发布同上的示例,我会更加感激。 谢谢

以下是填充表格布局的代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_selection);

        TableLayout tl = (TableLayout) findViewById(R.id.maintable);




        TextView TVdate = (TextView) findViewById(R.id.textView1_date2);

        Intent getStorei = getIntent();
        // UsrGPSLock = i.getStringExtra("currUsrLat") + "," +
        // i.getStringExtra("currUsrLon");
        userDate = getStorei.getStringExtra("userDate");
        TVdate.setText(userDate);

        TelephonyManager tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        uuid = tManager.getDeviceId();



        dbengine.open();

        storeList = dbengine.FetchStoreList();

        dbengine.close();

        /*final String[] storeCode = new String[storeList.length];
        final String[] storeName = new String[storeList.length];*/

        storeCode = new String[storeList.length];
        storeName = new String[storeList.length];

        for (int splitval = 0; splitval <= (storeList.length - 1); splitval++) {
            StringTokenizer tokens = new StringTokenizer(String.valueOf(storeList[splitval]), "_");

            storeCode[splitval] = tokens.nextToken().trim();
            storeName[splitval] = tokens.nextToken().trim();

        }

        System.out.println(storeList);

        // Get the TableLayout
        /*TableLayout tl = (TableLayout) findViewById(R.id.maintable);*/

        // Go through each item in the array
        for (int current = 0; current <= (storeList.length - 1); current++) {

            // Create a TableRow and give it an ID
            tr = new TableRow(this);

            tr.setId(200 + current);
            tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            tr.setClickable(true);

            RadioButton rb1 = new RadioButton(this);

            rb1.setId(current);
            rb1.setText(storeName[current]);
            rb1.setTextColor(Color.BLACK);
            rb1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            tr.addView(rb1);

            CheckBox cb1 = new CheckBox(this);
            cb1.setId(500 + current);
            // cb1.setText(provinces[current]);
            cb1.setTextColor(Color.BLACK);
            cb1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            tr.addView(cb1);

            /*
             * CheckBox cb2 = new CheckBox(this); cb2.setId(300+current);
             * //cb1.setText(provinces[current]); cb2.setTextColor(Color.BLACK);
             * cb2.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT,
             * LayoutParams.WRAP_CONTENT)); tr.addView(cb2);
             */

            // Add the TableRow to the TableLayout
            tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        }

        tr.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                System.out.println("inside-onClick");

                System.out.println(arg0.getId());
                System.out.println(arg0.getTag().toString());

                /*TableRow lTableRow = ((TableRow) arg0);
                RadioButton chkRB = (RadioButton) lTableRow.getChildAt(1);

                Toast.makeText(getApplicationContext(), lTableRow.toString(), Toast.LENGTH_SHORT).show();
                System.out.println(chkRB.getId());
                System.out.println(arg0.getTag().toString());*/

            }
        });

    }
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u store\u selection);
TableLayout tl=(TableLayout)findviewbyd(R.id.maintable);
TextView TVdate=(TextView)findViewById(R.id.textView1\u date2);
Intent getStorei=getIntent();
//UsrGPSLock=i.getStringExtra(“currUsrLat”)+,“+
//i.getStringExtra(“currUsrLon”);
userDate=getStorei.getStringExtra(“userDate”);
TVdate.setText(userDate);
TelephonyManager tManager=(TelephonyManager)getSystemService(Context.TELEPHONY_服务);
uuid=tManager.getDeviceId();
dbengine.open();
storeList=dbengine.FetchStoreList();
dbengine.close();
/*最终字符串[]storeCode=新字符串[storeList.length];
最终字符串[]storeName=新字符串[storeList.length]*/
storeCode=新字符串[storeList.length];
storeName=新字符串[storeList.length];

对于(int-splitval=0;splitval现在,当您将
OnClickListener
设置在
循环的
外部时,您只为
表格布局中的最后一个
表格行
设置了
OnClickListener
。移动:

tr.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            System.out.println("inside-onClick");

            System.out.println(arg0.getId());
            System.out.println(arg0.getTag().toString());

            /*TableRow lTableRow = ((TableRow) arg0);
            RadioButton chkRB = (RadioButton) lTableRow.getChildAt(1);

            Toast.makeText(getApplicationContext(), lTableRow.toString(), Toast.LENGTH_SHORT).show();
            System.out.println(chkRB.getId());
            System.out.println(arg0.getTag().toString());*/

        }
    });

内部for
循环的

您只需在for循环内部添加onClick侦听器,它就可以正常工作了

像这样:

for (int current = 0; current <= (storeList.length - 1); current++) {

            // Create a TableRow and give it an ID
            tr = new TableRow(this);
            tr.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                System.out.println("inside-onClick");

                System.out.println(arg0.getId());
                System.out.println(arg0.getTag().toString());

                /*TableRow lTableRow = ((TableRow) arg0);
                RadioButton chkRB = (RadioButton) lTableRow.getChildAt(1);

                Toast.makeText(getApplicationContext(), lTableRow.toString(), Toast.LENGTH_SHORT).show();
                System.out.println(chkRB.getId());
                System.out.println(arg0.getTag().toString());*/

            }
        });

}

for(int current=0;current Hi:D nope..在logcat中看不到任何*out。我猜事件根本没有被触发。@EmbeddedFander在哪里单击
TableRow
(添加行视图时,您应该真正使用
TableRow.LayoutParams
)?另外,您是否确定表顶部没有“吃掉”单击事件的内容(如另一个布局)?但是..在循环中移动单选按钮的OnClickListener可以:)是
TableRow.LayoutParams
已被使用..(请参见代码:
tr.setLayoutParams(新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
rb1.setLayoutParams(新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
。我的xml中有一个很深的布局结构(请参阅更新的问题)@EmbeddedFanizer在xml布局中,您将
表格布局的两列都设置为拉伸,这样您就没有任何地方可以单击来触发
表格行的
OnClickListener
(而是单击
单选按钮和
复选框
).你真的需要这两个小部件来填满一半的表格吗?我不知道为什么这个问题被否决了;这是一个3年前的问题。@downVoter如果你3年前有更好的提问/回答方式,那么请你这么做,我会很高兴地投你一票..而不是你这样到处否决
for (int current = 0; current <= (storeList.length - 1); current++) {

            // Create a TableRow and give it an ID
            tr = new TableRow(this);
            tr.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                System.out.println("inside-onClick");

                System.out.println(arg0.getId());
                System.out.println(arg0.getTag().toString());

                /*TableRow lTableRow = ((TableRow) arg0);
                RadioButton chkRB = (RadioButton) lTableRow.getChildAt(1);

                Toast.makeText(getApplicationContext(), lTableRow.toString(), Toast.LENGTH_SHORT).show();
                System.out.println(chkRB.getId());
                System.out.println(arg0.getTag().toString());*/

            }
        });

}