1

我已经完成了从表中派生数据的编码,并在 android 视图中显示为表列和行。

数据库编码是

public List<Country> getAllCountry()
{
    List<Country> countryList = new ArrayList<Country>();

    //select query
    String selectQuery = "SELECT * FROM COUNTRY_LIST";

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);


    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
    {
         Country country = new Country();
            country.setId(cursor.getString(0));
            country.setName(cursor.getString(1));
            country.setNationalty(cursor.getString(2));
            country.setDate(cursor.getString(3));


            // Adding person to list
            countryList.add(country);
    }

    return countryList;

}

XML 是

<TableLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tab">
  <TableRow
      >        
  </TableRow>    
</TableLayout>

活动编码是

List<Country> country = db.getAllCountry();

    StringBuilder builder = new StringBuilder();
    for (Country c: country)
    {               
        builder.append(c.getId()).append(";")
            .append(c.getName()).append(";")
            .append(c.getNationalty()).append(";")
            .append(c.getDate()).append("_");
    }
    //tv.setText(builder.toString());

    builder.toString();

    String st = new String(builder);
    Log.d("Main",st);
    String[] rows  = st.split("_");
    TableLayout tableLayout = (TableLayout)findViewById(R.id.tab);
    tableLayout.removeAllViews();

    for(int i=0;i<rows.length;i++){
        Log.d("Rows",rows[i]);
        String row  = rows[i];
        TableRow tableRow = new TableRow(getApplicationContext());
        tableRow.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        final String[] cols = row.split(";");

        Handler handler = null;

        for (int j = 0; j < cols.length; j++) {             
            final String col = cols[j];                                 
            TextView columsView = new TextView(getApplicationContext());
            columsView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
            columsView.setTextColor(color.black);
            columsView.setText(String.format("%7s", col));                                
            Log.d("Cols", String.format("%7s", col));
            tableRow.addView(columsView);

        }
    }

上面的代码没有在 android 模拟器屏幕上打印任何东西。

请说我做错了什么。

提前致谢。

问候, 迪内什

4

2 回答 2

1

我猜你在从第二个 for 循环出来后的第一个 for 循环的最后一行缺少 tableLayout.addView(tableRow) ..else 行将不会被添加..

于 2013-08-01T07:28:50.360 回答
0

我认为您很可能忘记调用此方法:

setContentView(<yourLayout>);

这将为您的活动设置布局。

注意:不要忘记测试您的是否List为空,以确保您有数据。

于 2013-08-01T07:05:05.700 回答