我有一个列表视图来显示一些项目,我也使用了一个空视图,以防万一我的适配器没有listview显示的项目。问题是当我首先执行这个活动时,它会在屏幕上显示第二个空白视图然后加载项目并在listview中显示它们。即使列表视图包含项目,为什么布局文件中的空视图显示在屏幕上?
我的动态onCreate
看起来是这样的:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.favorite_exams);
list = (ListView) findViewById(R.id.favoriteExamsList);
View empty = findViewById(R.id.favoriteExamsListEmptyView);
list.setEmptyView(empty);
new readingFavFileTask().execute(UtilityFuctions.FAV_EXAMS_FILE_NAME);
}
我的布局文件是:
<?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="vertical" >
<ListView
android:id="@+id/favoriteExamsList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
<LinearLayout
android:id="@+id/favoriteExamsListEmptyView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:visibility="gone" >
<TextView
android:id="@+id/favoriteExamsEmptyViewMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="You do not have any favorite Exams.Please add Exams using below button."
android:textSize="20dp" />
<Button
android:id="@+id/favoriteExamsAdd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="onClickAddExams"
android:text="Add Exams"
>
</Button>
</LinearLayout>
</LinearLayout>
如何我请务必注意空的观点一直没有出现,即使是第二,如果我们有数据我们的适配器。我应该检查适配器后附上空的视图列表以后或以其他任何方式做到这一点?
可能会显示空视图,因为任务没有完全立即设置ListView的适配器。除非您确切知道执行任务需要多少时间,否则您无能为力。 – Luksprog
如果只是做我的AsyncTask onPostExecute()方法list.setEmptyView(空),会不会有帮助? – Anshul
是的,在您首次在任务的'onPostExecute'中设置适配器后,设置空视图。这样,空白视图不会在第一次执行任务时出现。当然这意味着如果任务第一次运行,则需要10秒才能完成,在此期间用户将不会在屏幕上看到空白视图。 – Luksprog