无法在第二行中插入值:Android TableLayout

时间:2017-03-17 12:07:14

标签: java android

  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搜索一个课程时,它显示在第一行,但我不明白我怎么能进入第二行。我尝试过几件事,但它没有用。请帮帮我!

2 个答案:

答案 0 :(得分:0)

我真的不明白你的问题(你需要解释一下你的问题更清楚,你在做什么等等)但是这段代码:

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]);
除非您更改i的值,否则

将始终插入您的第一个表格行。如果i = 1,则会创建第二行。

你是不是喜欢下面的东西?

EditText et = (EditText)findViewById(R.id.et);//searching the course by it's id.
int id= Integer.parseInt(et.getText().toString());

int i =0;
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);

    LinearLayout ll = new LinearLayout(MainActivity.this); // You can set the orientation of this as desired using setOrientation
    ll.addView(textViewArray[0]);
    ll.addView(textViewArray[1]);
    ll.addView(textViewArray[2]);
    ll.addView(textViewArray[3]);
    ll.addView(textViewArray[4]);       
    tableRowArray[i].addView(ll);
    i++;
    }
}

注意,我已将i变量移到循环外部并在循环中递增。这就是你要追求的吗?

答案 1 :(得分:0)

感谢您的帮助,但我得到了解决方案。我需要动态创建行和textView,而不是在输入行中的值之前尝试创建它们,这是解决方案。 公共类MainActivity扩展了AppCompatActivity {

    TableLayout tableLayout;
    TableRow tr ;
    TextView tv1,tv2,tv3,tv4,tv5;
    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);


        Button button =(Button)findViewById(R.id.button2);

        button.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

             addData();

     }
        });


    }

    public void addData()
    {
        EditText et = (EditText)findViewById(R.id.et);
        int id= Integer.parseInt(et.getText().toString());

        LinkedList<Course> courses = cf.getCourses(id);



            for (Course c : courses) {

                if (c.getId() == id) {

                        tr = new TableRow(this);
                        tr.setLayoutParams(new LayoutParams(
                                LayoutParams.MATCH_PARENT,
                                LayoutParams.WRAP_CONTENT));

                    tv1 = new TextView(this);
                    tv1.setText("1");
                    tr.addView(tv1);

                    tv2 = new TextView(this);
                    tv2.setText(c.getTitile());
                    tr.addView(tv2);

                    tv3 = new TextView(this);
                        String credit = new Integer(c.getCredit()).toString();
                        tv3.setText(credit);
                        tr.addView(tv3);

                        tv4 = new TextView(this);
                        String tpc = new Integer(c.getTuitionPerCredit()).toString();
                        tv4.setText(tpc);
                        tr.addView(tv4);

                        tv5 = new TextView(this);
                        String st = new Integer(c.getSubtotal()).toString();
                        tv5.setText(st);
                        tr.addView(tv5);



                         tableLayout.addView(tr);
                }


        }
    }


}