簡體   English   中英

如何在Android中獲取表布局的內容

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

我正在嘗試制作一個表布局為3x3的應用程序(我已經用Java完成了)。 目前,我有3個活動; 活動A包含一個打算打開活動B的ImageView,該活動包含一個EditText,一個TableLayout(用TextViews填充)和帶有2 ImageView的LinearLayout。 一切都包裝在ConstraintLayout中。 TableLayout中的9個TextView均打開活動C,活動C是用於更新TableLayout的NumberPicker。

我的最終目標是通過維度數組將TableLayout內容獲取到。

我的問題是,一旦TableLayout填滿,如何獲取內容?

如您在活動B末尾的代碼中所見,我已經創建了一個PrintTable方法來打印Console的TableLayout內容。 我已經嘗試過,但是得到的TableLayout為null,因此無法訪問它。 老實說,我陷入了如何從表布局中獲取數據(如果已填滿)的問題,以及如何實現打印數據的方法。

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

   } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM