【发布时间】:2014-05-13 12:04:54
【问题描述】:
我正在创建一个 Android 应用程序,我必须在 Android 应用程序中显示 sqlite 的内容。
所以我决定在这里使用 TableLayout。但这里的重点是 sqlite 数据库中的列和行之前没有决定,所以我需要创建那种处理动态列和行的布局。
所以我在这里所做的是我创建了一个布局 sublayout.xml、cells.xml、mainlayout.xml。
sublayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/subLayout_" >
</LinearLayout>
在此布局中,我使用 cells.xml 动态添加单元格。
cells.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/cell_"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cell"
android:textSize="25dp"/>
</LinearLayout>
当我的一行准备好时,我只需将其添加到 mainlayout.xml。
mainlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/tableLayout_"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"></LinearLayout>
</TableRow>
</TableLayout>
但我在这里遇到空指针异常。
这是我的课程代码。
LinearLayout layout,subLayout_;
layout=(LinearLayout)findViewById(R.id.tableLayout_);
subLayout_=(LinearLayout)findViewById(R.id.subLayout_);
for (int i = 0; i < columns.length; i++) {
Log.e("Column name", ""+columns[i]);
View v=inflater.inflate(R.layout.cells, null);
TextView tv=(TextView)v.findViewById(R.id.cell_);
tv.setText(columns[i]);
subLayout_.addView(v);
}
layout.addView(subLayout_);
请帮助我在这里做错了什么?
【问题讨论】:
标签: android dynamic dynamic-programming tablelayout android-tablelayout