【发布时间】:2014-01-23 18:12:00
【问题描述】:
我编写了我的第一个 android 应用程序,简单的 CRUD。但是在我将 GridView 更改为 TableLayout 后,出现以下错误:
android.widget.TableRow cannot be cast to android.widget.TableLayout
这是我的活动:
public class AllActivity extends Activity {
private SQLiteDatabase sampleDB;
private TableLayout tableList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all);
sampleDB = this.openOrCreateDatabase("my_app",
SQLiteDatabase.CREATE_IF_NECESSARY, null);
String query = "SELECT * FROM BOOKS;";
Cursor cursor = sampleDB.rawQuery(query, null);
tableList = (TableLayout)findViewById(R.id.tableList);
if(cursor.moveToFirst())
{
do
{
TableRow row = new TableRow(this);
TextView id = new TextView(this);
id.setText(""+cursor.getLong(0));
TextView title = new TextView(this);
title.setText(cursor.getString(1));
TextView author = new TextView(this);
author.setText(cursor.getString(2));
row.addView(id);
row.addView(title);
row.addView(author);
tableList.addView(row);
}while(cursor.moveToNext());
}
}
}
我的看法:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableRow
android:id="@+id/tableList"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TableRow>
</LinearLayout>
任何提示我做错了什么?
我做了所有像 Print out ArrayList content in Android TableLayout 一样的事情,但无法正常工作。
【问题讨论】:
标签: android android-layout exception