2017-03-17 59 views
0
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

+0

你在哪裏更改變量「i」的值? – vadber

+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]); – Dia

+0

我希望它在完成第一課程和第一行後顯示第二行的詳細信息。但我無法做到這一點。我手動修復「我」= 0的值,看看它是否工作。現在我想去第二排。請幫我 – Dia

回答

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變量。那是你的追求?

+0

在我的代碼的這一部分,我在第一行添加了帶有值的textView。現在我的問題正是你所說的。我無法找到如何改變「我」的價值,這樣當我搜索新課程時,它會被添加到第二行。我試圖使用循環但id不起作用。 – Dia

+0

更新了我的答案。不確定它是否有幫助?! – Ricky

+0

我試過這個,它顯示這個錯誤:「指定的孩子已經有一個父親,你必須首先調用孩子父母的removeView():(」。 – Dia

0

謝謝你的幫助球員,但我得到了解決方案。我需要動態創建行和textView,而不是在輸入行中的值之前創建它們,這裏是解決方案。 public class MainActivity extends 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); 
       } 


     } 
    } 


}