简体   繁体   English

如何在Android中获取表布局的内容

[英]How to get the contents of a Table Layout in Android

I am trying to make an app(which I have already completed in Java) with a table layout 3x3. 我正在尝试制作一个表布局为3x3的应用程序(我已经用Java完成了)。 Currently, I have 3 Activities; 目前,我有3个活动; Activity A contains an ImageView that is meant to open Activity B, which contains an EditText, a TableLayout(filled with TextViews) and a LinearLayout with 2 ImageView. 活动A包含一个打算打开活动B的ImageView,该活动包含一个EditText,一个TableLayout(用TextViews填充)和带有2 ImageView的LinearLayout。 Everything is wrapped in a ConstraintLayout. 一切都包装在ConstraintLayout中。 Each of the 9 TextView in the TableLayout opens Activity C which is a NumberPicker to update the TableLayout. TableLayout中的9个TextView均打开活动C,活动C是用于更新TableLayout的NumberPicker。

My ultimate goal is to get the TableLayout contents to a by the dimensional array. 我的最终目标是通过维度数组将TableLayout内容获取到。

My question is How do I get the contents of the TableLayout once it is filled? 我的问题是,一旦TableLayout填满,如何获取内容?

As you can see in my code at the end of Activity B, I have made a method PrintTable to print to Console the contents of the TableLayout. 如您在活动B末尾的代码中所见,我已经创建了一个PrintTable方法来打印Console的TableLayout内容。 I have tried but I get that the TableLayout is null, therefore I cannot access it. 我已经尝试过,但是得到的TableLayout为null,因此无法访问它。 To be honest I'm stuck in how to get the data from the Table Layout once it if filled, how to implement the method to print it. 老实说,我陷入了如何从表布局中获取数据(如果已填满)的问题,以及如何实现打印数据的方法。

Activity A 活动A

public class MainActivity extends AppCompatActivity{
    ImageView button1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = (ImageView) findViewById(R.id.add_card);

    }

    public void openThreeByThreeCard(View v) {
        Intent intent = new Intent(this,ThreeByThree.class);
        startActivity(intent);
    }
}

Activity B 活动B

public class ThreeByThree extends AppCompatActivity {

    private ArrayList<Integer> A;
    private ArrayList<Integer> B;
    private ArrayList<Integer> C;
    TableLayout threeByThree;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        threeByThree;= (TableLayout) findViewById(R.id.Three_By_Three_Table);
        setContentView(R.layout.activity_three_by_three);
        A = new ArrayList<Integer>();
        B = new ArrayList<Integer>();
        C = new ArrayList<Integer>();

        for (int i = 1; i < 22; i++) {
            if (i > 0 && i < 8)
                A.add(i);
            if (i > 7 && i < 15)
                B.add(i);
            if (i > 14 && i < 22)
                C.add(i);
        }
    }

    public void openNumberPicker(View v) {

    //This opens a Number Picker to fill up the table one by one

        intent.putExtra("Cell ID", v.getId());
        startActivityForResult(intent, 17);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 17 ) {
            if (resultCode == RESULT_OK) {

                }
            } else if (resultCode == 5 && data.getStringExtra("Number to Change") != null) {
                                   }
                }
            } else if (resultCode == RESULT_CANCELED) {
                    }
                }
            }
        }
    }

    public void printTable(View v) {

    //This method is to get the contents of the table once it is filled

        String rowContent = null;
        for (int i = 0; i < threeByThree.getChildCount(); i++) {
            TableRow row = (TableRow) threeByThree.getChildAt(i);
            for (int j = 0; j < row.getChildCount(); j++) {
                TextView currentCell = (TextView) row.getChildAt(j);
                rowContent = currentCell.getText() + ", ";
            }
            Log.d("Content of Row is:", rowContent);
        }
    }
}

Regards Ne0R@Cu 关于Ne0R @ Cu

TableLayout is a LinearLayout (vertical) consisting of TableRow elements. TableLayout是由TableRow元素组成的LinearLayout(垂直)。 Each TableRow is another LinearLayout (horizontal) consisting of cell elements. 每个TableRow是由单元格元素组成的另一个LinearLayout(水平)。

So you can use getChildAt to get TableRow, then again getChildAt to get the cell from the row: 所以,你可以使用getChildAt得到的TableRow,然后再getChildAt获得来自该行的单元格:

   public View getTableLayoutCell(TableLayout layout, int rowNo, int columnNo) {
        if (rowNo >= layout.getChildCount()) return null;
        TableRow row = (TableRow) layout.getChildAt(rowNo);          

        if (columnNo >= row.getChildCount()) return null;
        return row.getChildAt(columnNo);

   } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM