如何在java / android中以矩阵形式显示数据

时间:2013-06-14 04:32:57

标签: java android matrix

我有以下格式的数据:

(0,0) -10
(1,0) - 20
(1,1) - 30

Medical- (0,0) -Jack
One medical Student
Engineer -  (1,0) - Jones
            (1,1) - Danny
    two Engineer Student

我试试这个:

for (int j = 0; j < Test.subNameArrayList.size(); j++) {

                        for (int i = 0; i < 2; i++) {
                            Subject[j] = new String[1];
                            Subject[j][i] = Test.subNameArrayList
                                    .get(j);
                            Name[j] = new String[2];
                            Name[j][i] = Test.NameArrayList
                                    .get(j);

                        }
                    }

如何在java和android中以矩阵形式显示。以及如何查找行大小和列大小。谢谢。

1 个答案:

答案 0 :(得分:0)

Xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
            <HorizontalScrollView
                android:id="@+id/horizontalScrollView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:orientation="horizontal" >

                    <TableLayout
                        android:id="@+id/tableLayout1"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent" >
                    </TableLayout>
                </LinearLayout>
            </HorizontalScrollView>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

活动:

public class TestActivity extends AppCompatActivity {
TableLayout tableLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        tableLayout = findViewById(R.id.tableLayout1);

        int[][] matrix = {
                {1,2,3},
                {4,5,6},
                {7,8,9}
        };
        fillTable(matrix,tableLayout);
    }
    private void fillTable(final  int[][] matrix, TableLayout table) {
        table.removeAllViews();
        for (int i = 0; i < matrix.length; i++) {
            TableRow row = new TableRow(TestActivity.this);
            row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
            for (int j = 0; j < matrix[i].length; j++) {
                TextView textView = new TextView(TestActivity.this);
               textView.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL|InputType.TYPE_NUMBER_FLAG_SIGNED);
                textView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
                textView.setText((matrix[i][j]+""));
                textView.setPadding(10,10,10,10);
                row.addView(textView);
            }
            table.addView(row);
        }
    }
}