public class MainActivity extends AppCompatActivity {
TableLayout tableLayout;
TextView[] textViewArray = new TextView[5];
TableRow[] tableRowArray = new TableRow[5];
TableRow tr1,tr2,tr3,tr4,tr5;
Course_Factory cf = new Course_Factory();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tableLayout = (TableLayout)findViewById(R.id.table);
for(int j =0 ;j<5;j++)
{
tableRowArray[j]=new TableRow(this);//Creating 5 textViews
textViewArray[j] = new TextView(this);//Creating 5 rows
tableLayout.setColumnStretchable(j,true);
}
Button button =(Button)findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText et = (EditText)findViewById(R.id.et);//searching the course by it's id.
int id= Integer.parseInt(et.getText().toString());
LinkedList<Course> courses = cf.getCourses(id);
for (Course c:courses)
{
if(c.getId()==id)//checking if id matches or not
{
textViewArray[0].setText("1");
textViewArray[1].setText(c.getTitile());
String credit = new Integer(c.getCredit()).toString();
textViewArray[2].setText(credit);
String tpc = new Integer(c.getTuitionPerCredit()).toString();
textViewArray[3].setText(tpc);
String st = new Integer(c.getSubtotal()).toString();
textViewArray[4].setText(st);
int i =0;
tableRowArray[i].addView(textViewArray[0]);
tableRowArray[i].addView(textViewArray[1]);
tableRowArray[i].addView(textViewArray[2]);
tableRowArray[i].addView(textViewArray[3]);
tableRowArray[i].addView(textViewArray[4]);
tableLayout.addView(tableRowArray[i]);
}
}
}
});
}
}
我已經在鏈接列表的另一個類中添加了一些課程。當我試圖通過它的id搜索一個課程時,它顯示在第一行,但我不明白我怎麼能去第二行。我嘗試了幾件事情,但沒有奏效。請幫幫我!無法在第二行中插入值:Android TableLayout
你在哪裏更改變量「i」的值? – vadber
tableRowArray [i] .addView(textViewArray [0]); tableRowArray [i] .addView(textViewArray [1]); tableRowArray [i] .addView(textViewArray [2]); tableRowArray [i] .addView(textViewArray [3]); tableRowArray [i] .addView(textViewArray [4]); tableLayout.addView(tableRowArray [i]); – Dia
我希望它在完成第一課程和第一行後顯示第二行的詳細信息。但我無法做到這一點。我手動修復「我」= 0的值,看看它是否工作。現在我想去第二排。請幫我 – Dia