2013-07-15 34 views
0

我真的需要幫助 我有一個ResultSet作爲從MySQL DB中選擇查詢的結果。我怎樣才能在表格佈局中顯示它?我爲桌面設置了佈局在表視圖中顯示結果集

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" android:layout_height="fill_parent" 
android:orientation="vertical"> 

<EditText 
    android:id="@+id/myFilter" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:hint="@string/some_hint" /> 
<TextView android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:padding="10dp" 
    android:text="@string/some_text" android:textSize="20sp" /> 
<TableLayout 
android:id="@+id/producttablelayout" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:dividerPadding="@dimen/activity_horizontal_margin" 
android:scrollbars="horizontal|vertical" 
android:showDividers="none|beginning|middle|end" > 

<TableRow 
    android:id="@+id/tableRow1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="top|bottom|left|right" 
    android:paddingTop="@dimen/activity_horizontal_margin" > 

    <TextView 
     android:id="@+id/ttt1" 
     android:text="Column 1" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/ttt2" 
     android:text="Column 12" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 


</TableRow> 

</TableLayout> 

</LinearLayout> 

我希望能夠將ResultSet數據添加到其中。喜歡的東西:

super.onCreate(savedInstanceState); 
    String s = null; 
    setContentView(R.layout.activity_list_all_products); 
    // Show the Up button in the action bar. 
    setupActionBar(); 
    context333=this; 
    TableLayout myTable = (TableLayout)findViewById(R.id.producttablelayout); 
    try 
    { 
    connect2 = DriverManager.getConnection(LogonActivity.url, LogonActivity.user, LogonActivity.password); 
    statement2 = connect2.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 
      ResultSet.CONCUR_READ_ONLY); 
    preparedStatement2 = connect2.prepareStatement("select  article_code,article_desc from products limit 4"); 
    resultSet2=preparedStatement2.executeQuery(); 
    resultSet2.beforeFirst(); 

    while (resultSet2.next()) { 
     how to add next record here to the table layout?? 

    } 

請幫

回答

0

您可以創建一個新的視圖(看來你是有TextViews填充你的錶行),從您的查詢的結果填充它們如你所願。然後,您可以使用構造函數創建一個新的TableRowsee the documentation)。

添加的意見,以錶行instnace使用:

tableRowInstance.addView(viewInstance);

,然後添加tableRowtableLayout以同樣的方式:

myTable.addView(tableRowInstance)

TableLayouts和TableRows都子看法。您應該對ViewGroup進行一些研究,查看以及它們的結構。 This link提供了很好的可視化。

+0

非常感謝您的回覆。 – Alex

+0

現在我有如下:'code'while(resultSet2.next()){ \t \t TextView text111 = new TextView(this); \t text111.setText(String.valueOf(resultSet2.getString(「article_code」))); TextView text222 = new TextView(this); text222.setText(String.valueOf(resultSet2.getString(「article_desc」))); TableRow tableRowInstance = new TableRow(this); for(int i = 1; i <3; i ++){TextView text = new TextView(this) ; text.setText(將String.valueOf(resultSet2.getString(I))); tableRowInstance.addView(文本);} myTable.addView(tableRowInstance); 如何預先定義該行的佈局?在XML中? – Alex

+0

@Alex,很難閱讀註釋部分的代碼。如果我的回答充分解決了您的原始問題,請將其除外並將上述代碼作爲新問題發佈。這對你和SO社區都有好處,因爲每個人都有機會回答你的下一個問題,可能有相同問題的人可以高效地進行搜索。謝謝 – Scott