简体   繁体   中英

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. Currently, I have 3 Activities; 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. Everything is wrapped in a ConstraintLayout. Each of the 9 TextView in the TableLayout opens Activity C which is a NumberPicker to update the TableLayout.

My ultimate goal is to get the TableLayout contents to a by the dimensional array.

My question is How do I get the contents of the TableLayout once it is filled?

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. I have tried but I get that the TableLayout is null, therefore I cannot access it. 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

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

Regards Ne0R@Cu

TableLayout is a LinearLayout (vertical) consisting of TableRow elements. Each TableRow is another LinearLayout (horizontal) consisting of cell elements.

So you can use getChildAt to get TableRow, then again getChildAt to get the cell from the row:

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

   } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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