【问题标题】:How to display data in matrix form in java/android如何在java/android中以矩阵形式显示数据
【发布时间】:2013-06-14 04:32:57
【问题描述】:

我有以下格式的数据:

(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中以矩阵形式显示。以及如何查找行大小和列大小。提前致谢。

【问题讨论】:

  • 可以使用arrays,Vectors,ArrayList来存储数据。
  • 我认为用户想要显示它们,而不是存储日期(问题真的太模糊了)但是这个其他问题可能会有所帮助:stackoverflow.com/questions/12161611/…

标签: java android matrix


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 2017-04-25
    • 1970-01-01
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多