开发者

Moving dynamic TableLayout methods to separate class

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 w开发者_如何学编程onderfully, 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
}


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);


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
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜