如何获得我的Andr​​oid表视图的列从数据库中的数据 [英] How to get data from database in my android table view as columns

查看:160
本文介绍了如何获得我的Andr​​oid表视图的列从数据库中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了以下编码导出数据表的,并在Android设备上查看显示为表列和行。

I have done following coding for deriving data's from table and displayed in the android view as a Table columns and rows.

数据库编码是

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);

        }
    }

以上code不打印在Android模拟器的屏幕东西。

The above code not printing anything in the android emulator screen.

请说什么我做了错误的。

Please say what have I done wrong.

先谢谢了。

问候,
迪内希

Regards, Dinesh

推荐答案

我猜你是走出来的第2次为loop..else一行之后一日for循环的最后一行缺少tableLayout.addView(tablerow的)不会加..

I guess you are missing tableLayout.addView(tableRow) on the last line of the 1st for loop after coming out of 2nd for loop..else row will not be added..

这篇关于如何获得我的Andr​​oid表视图的列从数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆