我不知道是什么问题,因为你给了我们代码并且没有错误,在 eclipse -> Window -> Open Perspective -> DDMS。当你用力关闭时,看看日志猫。无论如何。这是我朋友的一个例子。
主列表 XML...您可以在布局中的任何位置添加列表视图...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="#FFFFD0"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@android:id/list"
android:cacheColorHint="#666666"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView android:id="@android:id/empty"
android:layout_width="wrap_content"
android:textColor="#4B89E0"
android:layout_height="wrap_content"
android:text="The list is empty! Do I need this? No idea!"
/>
</LinearLayout>
下一行:
现在这些可能会变得非常复杂。我有一个包含 5 个图像和 3 个文本视图的列表的程序,android 似乎可以很好地处理它。但这是一个基本的,3 个文本视图...也可以与一个一起使用。
<?xml version="1.0" encoding="utf-8"?>
<!-- row.xml -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:textAppearance="?android:attr/textAppearanceLarge"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/text1"
android:textColor="#4B89E0"
android:layout_width="130dip"
android:layout_height="40dip"
android:textSize = "15dip"
/>
<TextView android:id="@+id/text2"
android:textColor="#4B89E0"
android:layout_width="70dip"
android:gravity = "center"
android:textStyle = "bold"
android:layout_height="wrap_content" />
<TextView android:id="@+id/text3"
android:textColor="#4B89E0"
android:gravity = "center"
android:layout_width="80dip"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
最后是一些代码把它放在一起......在这个例子中调用fillData();填写清单。我通常喜欢这样做,这样当我更改数据库时,我正在检索哪些信息(即,我想查看:所有形状或圆形或方形)列表很容易更新。我假设您的数据库已经打开。您在 OnCreate() 中打开了它。
与其在 OnCreate 方法中创建所有这些代码,不如创建自己的方法“fillData”,而您在 OnCreate 中所拥有的只是 fillData();。这将使查看、编辑和故障排除变得更加容易。
private void fillData() {
Cursor fetchInfo = mDbHelper.fetchAllRecords();
startManagingCursor(fetchInfo);
// Create an array to specify the fields we want to display in the list (TITLE,DATE,NUMBER)
String[] from = new String[]{DbAdapter.KEY_TITLE,
DbAdapter.KEY_DATE, DbAdapter.KEY_AUTHOR };
// an array of the views that we want to bind those fields to (in this case text1,text2,text3)
int[] to = new int[]{R.id.text1, R.id.text2, R.id.text3};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.list_row, fetchInfo, from, to);
setListAdapter(notes);
}
WakaWaka
祝你好运