2017-05-30 83 views
0

我想用表格佈局3x3製作應用程序(我已經用Java完成了)。目前,我有3項活動; 活動A包含是指以打開活動B,其中包含一個EditText,一個TableLayout(填充有TextViews)中並用2 ImageView的一個的LinearLayout一個ImageView的。一切都包裹在一個ConstraintLayout中。 TableLayout中的每個TextView都打開Activity C,它是一個NumberPicker來更新TableLayout。如何在Android中獲取表格佈局的內容

我的最終目標是在維數組,以獲得TableLayout內容的。

我的問題是我如何獲得TableLayout的內容,一旦存滿了?

正如你可以在活動B結束在我的代碼看,我做了一個方法PrintTable打印到控制檯TableLayout的內容。我試過了,但是我得到TableLayout爲空,因此我無法訪問它。 老實說,我被卡在如何從表格佈局中獲取數據,一旦它被填充,如何實現打印它的方法。

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

活動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 @銅

+0

所以最後我發現我一直在尋找。 我沒有在我的方法中獲得對我的TableLayout的引用,但在該類的外部。一旦我將引用作爲局部變量插入到我的** _ printTable _ **方法中,我就能夠實現該方法以在填充後獲取表格的內容。 – Racu

回答

0

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

    } 
+0

謝謝@cyanide,或許你能幫助我理解。在活動B中,我填充表格。我如何在活動B中訪問它,或者將它作爲表格返回到活動A.因爲這是我在屏幕上的問題,所以我可以看到我填充的表格,但是在代碼中,我不知道如何訪問它。無論我嘗試獲取對錶的引用,我都會得到一個「null」,儘管我可以「看到」我的表完整。謝謝 – Racu