将动态 TableLayout 方法移至单独的类
我有一个活动需要对 TableLayout 进行各种条件更新(执行/不显示具有“0”值的行、创建新的行项目...等)。我的 Dynamic TableLayout 工作得非常好,但是正如你可以想象的那样,它的排列还在继续增长。我想将各种 TableRow 管理方法移到一个单独的类中,但在转换时遇到问题。
第一个问题是当我尝试从我的主要活动中调用 BuildTable.testTable();
时,它希望该方法是静态的。这是有道理的,但是当我将 testTable 设为静态时,我收到抱怨“无法从 Activity 类型对非静态方法 findViewById(int)
进行静态引用”。当我遵循建议 这里,但它只是没有完全结合在一起。我需要帮助...并且真诚地感谢您提供的任何帮助。
我将其归结为以下基础知识,只插入了一个 TextView...我有:
public class BuildTable extends Activity {
public void testTable() {
TableLayout tl = (TableLayout)findViewById(R.id.InvoiceTable); // Find TableLayout defined in main.xml
TableRow trDivider = new TableRow(getParent());
TextView tvDivider = new TextView(getParent()); //Create a divider view between rows
tvDivider.setText("test");
trDivider.addView(tvDivider); //Add tvDivider to the new row
tl.addView(trDivider); //Add trDivider to the TableLayout
}
I have an activity that requires various conditional updates to a TableLayout (do/don't show lines with a '0' value, create a new line item... etc). I have the Dynamic TableLayout working wonderfully, but as you can imagine the permutations of this continue to grow. I want to move the various TableRow management methods out to a separate class but am having trouble with the transition.
The first problem is when I attempt to call BuildTable.testTable();
from my main activity it wants the method to be static. This makes sense but when I make testTable static, then I get the complaint "cannot make a static reference to the non-static method findViewById(int)
from the type Activity". It seemed like I drew very close to a solution when I followed the advice here, but it just didn't quite come together. I need help... and would sincerely appreciate anything you can offer.
I have boiled it down to the basics below with only a single TextView inserted... I have:
public class BuildTable extends Activity {
public void testTable() {
TableLayout tl = (TableLayout)findViewById(R.id.InvoiceTable); // Find TableLayout defined in main.xml
TableRow trDivider = new TableRow(getParent());
TextView tvDivider = new TextView(getParent()); //Create a divider view between rows
tvDivider.setText("test");
trDivider.addView(tvDivider); //Add tvDivider to the new row
tl.addView(trDivider); //Add trDivider to the TableLayout
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您确实需要一个
static
方法来执行此操作,只需将您的Activity
传递到方法调用中即可。然后在您的
Activity
中使用:If you really want a
static
method for doing this, just pass yourActivity
into the method call.Then in your
Activity
use:为什么要静态调用这个函数呢?
如果您只是想分离某些功能,则似乎没有必要实际扩展活动。称其为非静态,并使用对您的活动的引用可能是一件事,如下所示:(快速输入,尚未通过编译器运行它来查看我的语法在哪里搞砸了:P)
Why do you want to call this function statically?
It does not seem necessary to actually extend activity, if you are just wanting to separate some of the functions. It might be a thing to call it non-static, and use a reference to your activity, like so: (quick type-up, haven't run it trough a compiler to see where I messed up on my syntax :P )
它要求该方法是静态的,因为您通过其类 BuildTable.testTable() 调用该方法;如果您要创建 BuildTbale 类的实例,那么它不必是静态的。
因此,在您的主要活动中,您会说
BuildTable bt = new BuildTable();
bt.testTable(this);.....
但是,在您的情况下,我认为您只需要创建一个方法而不是一个新类,除非您要从多个活动中调用它或者您想要创建多个同一类的一个实例。
It asks for the method to be static because you are calling this by its class BuildTable.testTable(); If you would make an instance of your BuildTbale class then it would not have to be static.
So in your main activity you would say
BuildTable bt = new BuildTable();
bt.testTable(this);.....
However, in your case I think you just need to create a method not a new class, unless you are going to call it from more than one activity or you want to create more than one instance of the same class.