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

How to get the contents of a Table Layout in Android

我正在尝试使用 table 布局 3x3 制作一个应用程序(我已经在 Java 中完成)。目前,我有 3 个活动; Activity A 包含一个用于打开 Activity B 的 ImageView,其中包含一个 EditText、一个 TableLayout(填充有 TextView)和一个带有 2 个 ImageView 的 LinearLayout。一切都包装在 ConstraintLayout 中。 TableLayout 中的 9 个 TextView 中的每一个都会打开 Activity C,这是一个用于更新 TableLayout 的 NumberPicker。

我的最终目标是将TableLayout内容通过维数组获取到a

我的问题是Table布局填充后如何获取它的内容?

正如您在 Activity B 末尾的代码中看到的那样,我创建了一个方法 PrintTable 来将内容打印到控制台Table布局的。我试过了,但我发现 TableLayout 为空,因此我无法访问它。 老实说,我被困在如何从 Table 布局中获取数据,一旦它被填充,如何实现打印它的方法。

Activity一个

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

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

问候 Ne0R@Cu

TableLayout 是由 TableRow 元素组成的 LinearLayout(垂直)。每个 TableRow 都是由单元格元素组成的另一个 LinearLayout(水平)。

因此您可以使用 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);

   }