[英]Android Creating Table Programmatically
I am trying to generate a number table with given row and column number. 我正在尝试生成具有给定行数和列数的数字表。 The program takes two parameters from the user and creating a table rows and columns with given parameters. 该程序从用户那里获取两个参数,并使用给定的参数创建表的行和列。 Each cell is going to have a random number value between 1 to 100. As far as, I coded my XML with 2 different EditText views which are going to be my parameters. 每个单元格将具有1到100之间的随机数。到目前为止,我用2个不同的EditText视图编码了XML,这将成为我的参数。 There are four buttons "Create", "Calculate", "Reset", "Exit" that I am going to use. 我将使用四个按钮“创建”,“计算”,“重置”,“退出”。 When the user clicks "Create" button, table will be generated with the given row and column values. 当用户单击“创建”按钮时,将使用给定的行和列值生成表。 Therefore I have to create my table from my java file instead of XML file. 因此,必须从Java文件而不是XML文件创建表。
This is my code so far: 到目前为止,这是我的代码:
public class MainActivity extends AppCompatActivity {
private Button btnCreate, btnCalculate, btnReset, btnExit;
private EditText txtColumn, txtRow;
private LinearLayout linLayout;
private TableLayout table;
private TextView txtResult;
private Random random;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCreate = (Button) findViewById(R.id.btnCreate);
btnCalculate = (Button) findViewById(R.id.btnCalculate);
btnReset = (Button) findViewById(R.id.btnReset);
btnExit = (Button) findViewById(R.id.btnExit);
txtColumn = (EditText) findViewById(R.id.txtColumn);
txtRow = (EditText) findViewById(R.id.txtRow);
txtResult = (TextView) findViewById(R.id.txtResult);
txtColumn.requestFocus();
random = new Random();
TableLayout table = new TableLayout(this);
btnCalculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
btnCreate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
table.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
table.setShrinkAllColumns(true);
table.setStretchAllColumns(true);
int columnNumber = Integer.parseInt(txtColumn.getText().toString());
int rowNumber = Integer.parseInt(txtRow.getText().toString());
for(int i=0; i<rowNumber; i++) {
TableRow row = new TableRow();
for(int j=0; i<columnNumber; i++) {
int value = random.nextInt(100) + 1;
TextView tv = new TextView();
tv.setText(String.valueOf(value));
row.addView(tv);
}
table.addView(row);
}
}
});
btnExit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
System.exit(0);
}
});
btnReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtColumn.setText(null);
txtRow.setText(null);
txtResult.setText(null);
}
});
}
}
I am getting an error at my "for" loops. 我的“ for”循环出现错误。 It says "TableRow" has to get Context parameter inside. 它说“ TableRow”必须在内部获取Context参数。 However, I really didn't understand what kind of parameter it wants. 但是,我真的不明白它想要什么样的参数。 I tried to search for table creation programmatically, however no luck so far. 我尝试以编程方式搜索表创建,但是到目前为止还没有运气。 How am I supposed to fix this error? 我应该如何解决此错误?
You need to pass context while creating a table row. 创建表行时需要传递上下文。 try below code 尝试下面的代码
for (int i=0; i < rowNumber; i++) {
TableRow row = new TableRow(MainActivity.this);
for (int j=0; j < columnNumber; j++) {
int value = random.nextInt(100) + 1;
TextView tv = new TextView(MainActivity.this);
tv.setText(String.valueOf(value));
row.addView(tv);
}
table.addView(row);
}
And you also need to add tablelayout to any view of xml for example in your xml file add liner layout like below 而且您还需要将tablelayout添加到xml的任何视图中,例如在xml文件中添加如下所示的衬板布局
<LinearLayout
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/>
Cast that view in java file like below 在Java文件中投射该视图,如下所示
LinearLayout mainLayout = findViewById(R.id.mainLayout);
Add your table layout to this view like below 将您的表格布局添加到此视图,如下所示
mainLayout.addView(table);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.