android表格布局与表读行

时间:2017-02-10 17:06:35

标签: java android android-studio android-tablelayout

我在表格布局中有表行,我需要读取所有行并将数据移动到sql。我如何逐行读取行需要读取第一行并转到第2行并阅读

插入表格行的代码:

TableLayout l1 = (TableLayout) findViewById(R.id.table1);
l1.setStretchAllColumns(true);
l1.bringToFront();

TableRow tr0 = new TableRow(getBaseContext());

TextView tv1 = new TextView(getBaseContext());
TextView tv2 = new TextView(getBaseContext());
TextView tv3 = new TextView(getBaseContext());
TextView tv4 = new TextView(getBaseContext());
TextView tv5 = new TextView(getBaseContext());

tv1.setTextColor(Color.BLACK);
tv2.setTextColor(Color.BLACK);
tv3.setTextColor(Color.BLACK);
tv4.setTextColor(Color.BLACK);
tv5.setTextColor(Color.BLACK);

tv1.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv2.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv3.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv4.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv5.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

tv1.setText(Integer.toString(id_row=id_row+1));
tv2.setText(e_entrbond_calcname.getText().toString());
tv3.setText(e_entrybonds_mdin1.getText().toString());
tv4.setText(e_entrybonds_dain1.getText().toString());
tv5.setText(e_entrybonds_details2.getText().toString());


tr0.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));

tr0.addView(tv1);
tr0.addView(tv2);
tr0.addView(tv3);
tr0.addView(tv4);
tr0.addView(tv5);

l1.addView(tr0);

1 个答案:

答案 0 :(得分:1)

尝试使用此代码从TableRow中读取值;

TableLayout l1 = (TableLayout) findViewById(R.id.table1);

for (int i = 0; i < l1.getChildCount(); i++) {
    View child = l1.getChildAt(i);

    if (child instanceof TableRow) {
        TableRow row = (TableRow) child;

        for (int x = 0; x < row.getChildCount(); x++) {
            //View view = row.getChildAt(x);
            TextView text = (TextView)row.getChildAt(x); // get child index on particular row
            String title = text.getText().toString();
            Log.i("Value", title);      
        }
    }
}