ListView - SimpleCursorAdapter语言 - @android:id/empty



t我有一个ListActivity。ListActivity的布局是

<ListView android:id="@android:id/list"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:drawSelectorOnTop="false"
           />
<TextView android:id="@android:id/empty"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:text="No elements found"
           android:gravity="center"  
           />

ListActivity与CursorAdapter相关。

当用户选择列表中的一个项目时,我会启动另一个活动来显示所选项目的详细信息:

Intent intent = new Intent(MyListActivity.this, DetailActivity.class);
startActivity(intent);

除了用户选择列表中的某个项目外,其他一切都很好。在开始"详细活动"之前,会显示id为"@android:id/empty"的textview的"No elements found"消息。

发生这种情况是因为我在onPause方法中关闭了光标。但我认为我必须关闭它,因为我要离开当前的活动。

@Override
protected void onPause()
{
    super.onPause();            
    if (this.cursor != null)
        this.cursor.close();
    this.db.close();        
}

如果我不想在离开当前活动时看到带有"@android:id/empty"的文本视图,我该怎么办??

感谢

让系统为您管理它。

getActivity().startManagingCursor(yourCursor);

在调用游标后立即调用该代码(其中包含正确的游标变量名),系统将负责为您管理游标,并在认为不再需要时关闭它。

您的另一个选择是将该代码移动到onStop方法,该方法在用户不再看到活动时调用。

最新更新