这是动态创建功能可点击TableRows [英] Functional Clickable TableRows that are created dynamically

查看:134
本文介绍了这是动态创建功能可点击TableRows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到这个链接(http://stackoverflow.com/questions/6603868/android-onclicklistener-and-table-layout)这似乎是它为问这个问题的人。话虽这么说,这里是我的情况。

I saw this link (http://stackoverflow.com/questions/6603868/android-onclicklistener-and-table-layout) which seems like it works for the person asking the question. That being said, here's my situation.

我有一个TableLayout,动态填充数据的四列。该行作为一个整体需要可以点击的,因为有像上面链接的例子没有任何按键。

I have a TableLayout, dynamically filled with four columns of data. The row as a whole needs to be clickable, since there are no buttons like the example linked above.

一个点击行需要通过它的第一列的(列0)的数据,这仅仅是一个字符串。这里是被称为创造一个新行的功能。

A clicked row needs to pass its first column's(column 0) data, which is just a string. Here is the function that's called to create a new row.

private void addLotTableRow(CharSequence [] row, int count) {
    final TableLayout table = (TableLayout) findViewById(R.id.lotsTableList);
    final TableRow tr = (TableRow) getLayoutInflater().inflate(R.layout.lotsrow, null);
    TextView tv;
    // Fill Cells
        tv = (TextView) tr.findViewById(R.id.lotCell1);//Cell 1: Lot Number
        tv.setText(row[0]);

        tv = (TextView) tr.findViewById(R.id.lotCell2);//Cell 2: Sample
        tv.setText(row[1]);

        tv = (TextView) tr.findViewById(R.id.lotCell3);//Cell 3: Inspected
        tv.setText(row[3]);

        tv = (TextView) tr.findViewById(R.id.lotCell4);//Cell 4: Total
        tv.setText(row[2]);

        table.addView(tr);
}

所以,我本来试图使tr.setOnClickListener(新View.OnclickListener(){blahblah});在这个函数中,对前table.addView(TR)线。该等等等等当然是一个onClick(视图V)函数。我原本只能取得最后一行的数据,无论我点击哪一行。现在,我试图做类似上面的链接,并从上涨的功能产生onclicks,所有的表行已被创建之后。我大概可以给更多的信息,但我认为人们可以从这个的想法现在!感谢您的帮助!

So I originally tried to make a tr.setOnClickListener(new View.OnclickListener() {blahblah}); in this function, right before the table.addView(tr) line. The "blah blah" was of course an onClick(View v) function. I originally would only get the last row's data, no matter which row I clicked. Now, I'm trying to do like the link above, and produce the onclicks from a higher up function, after all the table rows have been created. I probably can give more info, but I think people can get the idea for now from this! Thanks for any help!

推荐答案

底线:我想它了。

这是我张贴在问题的链接立足我的解决方案,我意识到我需要做出一个TextView对象,以看到一个单元格的数据!因此,这里是我的code,在连接到code上面一直没有改变!

Basing my solution from the link I posted in the question, I realized that I needed to make a textview object in order to see a cell's data! So here is my code, in connection to the code above that has remained unchanged!

    int rowNumCount = table.getChildCount();
    for(int count = 1; count < rowNumCount; count++) {
        View v = table.getChildAt(count);
        if(v instanceof TableRow) {
            final TableRow clickRow = (TableRow)v;
            int rowCount = clickRow.getChildCount();
            v.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Context context = getTabHost().getContext();
                    TableRow row = (TableRow)v;
                    TextView tv = (TextView)row.getChildAt(0);
                    CharSequence text = "Lot VALUE Selected: " + tv.getText();
                    int duration = Toast.LENGTH_SHORT;
                    Toast.makeText(context, text, duration).show();
                }
            });
        }
    }

就像我说的,我只需要抢到第一列中的数据,从而row.getChildAt(0);线!所以我知道没有人甚至可能不得不回答这个问题还没有机会,但我希望我的回答可以帮助别人的未来!

Like I said, I only needed to grab the first columns data, thus the row.getChildAt(0); line! So I know nobody probably even had the chance to answer this question yet, but I hope my answer can help others in the future!

理的问题,为什么不直接使用一个ListView?
答:我觉得我在做什么,以表格的形式看起来好多了。

Rationale to the question, "Why not just use a listview?" Answer: I think for what I'm making, a tabular format looks much better.

虽然我可能不会让我的code的设置一个巨大的变化,我总是开听到的变化,可以帮助改善我的code!我爱这个社会!

While I may not make a huge change to my code's setup, I am ALWAYS open to hear changes that could help improve my code! I love this community!

这篇关于这是动态创建功能可点击TableRows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆