2013-06-07 3 views
1

데이터베이스에서 내용을 가져 와서 테이블 레이아웃에 행을 추가하고 싶습니다. 아래 코드를 시도했지만 작동하지 않습니다. 나는, 데이타베이스 관련 업무에 대한 클래스 'ProductDatabase'을 만들어 ... 그리고 다른 활동에 아래의 코드를 사용하여 ...하지만 그 어떤 출력TableLayout에 동적으로 행을 추가하십시오.

ProductDatabase pData; //Database Class 
TableLayout mainTable; 
Cursor cr; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.display_product_database); 

    pData = new ProductDatabase(this); 
    mainTable = (TableLayout) findViewById(R.id.mainTable); 


} 

@SuppressWarnings("deprecation") 
@Override 
protected void onResume() { 
    super.onResume(); 
    pData.open(); 
    cr=pData.getDatabaseCursor(); //fetching cursor from Database Class 
    int index=1;  
     for (cr.moveToFirst(); !cr.isAfterLast(); cr.moveToNext()) { 
      TableRow tr=new TableRow(this); 
       tr.setId(1000+index); 
       tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 

       TextView labelName=new TextView(this); 
       labelName.setId(2000+index); 
       labelName.setText(cr.getString(cr.getColumnIndex(ProductDatabase.KEY_NAME))); 
       labelName.setTextColor(Color.BLACK); 
       labelName.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
       tr.addView(labelName); 

       TextView labelBarcode=new TextView(this); 
       labelBarcode.setId(3000+index); 
       labelBarcode.setText(cr.getString(cr.getColumnIndex(ProductDatabase.KEY_BARCODE))); 
       labelBarcode.setTextColor(Color.BLUE); 
       labelBarcode.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
       tr.addView(labelBarcode); 

       TextView labelCost=new TextView(this); 
       labelCost.setId(4000+index); 
       labelCost.setText(cr.getString(cr.getColumnIndex(ProductDatabase.KEY_COST))); 
       labelCost.setTextColor(Color.BLUE); 
       labelCost.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
       tr.addView(labelCost); 

       mainTable.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 

       index++; 

          } 
    pData.close(); 

} 

}

답변

0

가 동적으로 행을 추가하려면 표시되지 TableLayout이 코드를 사용할 수

for (int i=0; i < 5; i ++) { 
     TableRow tr=new TableRow(this); 
     tr.setId(1000 + i); 

     TextView labelName=new TextView(this); 
     labelName.setId(2000 + i); 
     labelName.setText("Test"); 
     labelName.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); 
     tr.addView(labelName); 

     TextView labelBarcode=new TextView(this); 
     labelBarcode.setId(3000+i); 
     labelBarcode.setText("Test"); 
     labelBarcode.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); 
     tr.addView(labelBarcode); 

     TextView labelCost=new TextView(this); 
     labelCost.setId(4000+i); 
     labelCost.setText("Test"); 
     labelCost.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); 
     tr.addView(labelCost); 

     mainTable.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT)); 

    } 

난 그냥 그것을 테스트하기 위해 조금 당신의 코드를 변경했습니다.

여기서 중요한 것은 LayoutParams에 영향을 미칩니다. 행을 추가 할 때 TableRowTableLayout.LayoutParams 내에서보기에 TableRow.LayoutParams을 사용합니다.

0
//this layout parem for the alignment of the row 
    TableRow.LayoutParams rowParams = 
          new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 0, 1f); 
       TableRow.LayoutParams itemParams = 
          new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
                TableRow.LayoutParams.MATCH_PARENT, 1f); 

       //making the loop for multiple table row 
       //start 
       for(int i=0;i<tid;i++){ 
       TableRow tr=new TableRow(this); 
       tr.setLayoutParams(rowParams); 
       j++; 
       TextView feel_sleep=new TextView(this); 
       TextView woke_up=new TextView(this); 
       TextView slept_min=new TextView(this); 
       //creating the add icon 
       ImageView icon_add=new ImageView(this); 
       icon_add.setBackgroundResource(R.drawable.icon_add); 

       //creating the delete icon 
       ImageView icon_delete=new ImageView(this); 
       icon_delete.setBackgroundResource(R.drawable.icon_delete); 

       LinearLayout icon_layout=new LinearLayout(this); 

       icon_layout.addView(icon_add); 
       icon_layout.addView(icon_delete); 


       TableRow.LayoutParams params = new TableRow.LayoutParams(
         LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
        params.gravity = Gravity.CENTER; 


       feel_sleep.setLayoutParams(itemParams); 
       woke_up.setLayoutParams(itemParams); 
       slept_min.setLayoutParams(itemParams); 
       icon_layout.setLayoutParams(params); 
       icon_layout.setId(j); 

       Logger.d("id for i:","**:"+i);  
       Logger.d("id for j:","**:"+j);  

       feel_sleep.setGravity(Gravity.CENTER); 
       woke_up.setGravity(Gravity.CENTER); 
       slept_min.setGravity(Gravity.CENTER); 


       feel_sleep.setText("00:00"); 
       woke_up.setText("00:00"); 

       tr.addView(feel_sleep); 
       tr.addView(woke_up); 
       tr.addView(slept_min); 
       tr.addView(icon_layout); 
    } 

    //this is the tablelayout id `enter code here` 
    table_layout.addView(tr); 
관련 문제