将动态 TableLayout 方法移至单独的类

发布于 2024-10-17 03:26:39 字数 1092 浏览 2 评论 0原文

我有一个活动需要对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江湖正好 2024-10-24 03:26:39

如果您确实需要一个static 方法来执行此操作,只需将您的Activity 传递到方法调用中即可。

public class BuildTable {

    private BuildTable (){
        // private as no need to create an instance
    }

    public static void testTable(Activity contextActivity) {
        TableLayout tl = (TableLayout) contextActivity.findViewById(R.id.InvoiceTable); // Find TableLayout defined in main.xml
        // etc
    }
}

然后在您的 Activity 中使用:

BuildTable.testTable(this);

If you really want a static method for doing this, just pass your Activity into the method call.

public class BuildTable {

    private BuildTable (){
        // private as no need to create an instance
    }

    public static void testTable(Activity contextActivity) {
        TableLayout tl = (TableLayout) contextActivity.findViewById(R.id.InvoiceTable); // Find TableLayout defined in main.xml
        // etc
    }
}

Then in your Activity use:

BuildTable.testTable(this);
神仙妹妹 2024-10-24 03:26:39

第一个问题是当我尝试
调用 BuildTable.testTable();从我的
它想要该方法执行的主要活动
保持静态

为什么要静态调用这个函数呢?

如果您只是想分离某些功能,则似乎没有必要实际扩展活动。称其为非静态,并使用对您的活动的引用可能是一件事,如下所示:(快速输入,尚未通过编译器运行它来查看我的语法在哪里搞砸了:P)

public class BuildTable { //doesn't need activity
private Activity contextActivity; //here is your activity


public BuildTable(Activity yourContext){     //build a constructor
    contextActivity = yourContext;
}

public void  testTable() {
    TableLayout tl = (TableLayout)contextActivity.findViewById(R.id.InvoiceTable); 
        TableRow trDivider = new TableRow(getParent());
            TextView tvDivider = new TextView(getParent()); 
            tvDivider.setText("test");
        trDivider.addView(tvDivider); //Add tvDivider to the new row
    tl.addView(trDivider); //Add trDivider to the TableLayout
}

The first problem is when I attempt to
call BuildTable.testTable(); from my
main activity it wants the method to
be static

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 )

public class BuildTable { //doesn't need activity
private Activity contextActivity; //here is your activity


public BuildTable(Activity yourContext){     //build a constructor
    contextActivity = yourContext;
}

public void  testTable() {
    TableLayout tl = (TableLayout)contextActivity.findViewById(R.id.InvoiceTable); 
        TableRow trDivider = new TableRow(getParent());
            TextView tvDivider = new TextView(getParent()); 
            tvDivider.setText("test");
        trDivider.addView(tvDivider); //Add tvDivider to the new row
    tl.addView(trDivider); //Add trDivider to the TableLayout
}
煮酒 2024-10-24 03:26:39

它要求该方法是静态的,因为您通过其类 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文