ListView页脚在第二次添加时不会出现

时间:2014-03-19 04:45:38

标签: android listview footer

我在ListView中显示页脚时遇到了一些奇怪的问题。这是场景:

我制作了一个应用程序,用于从服务器加载 news 。 当我将屏幕向下滚动到其中的最后一个项目时,应用程序正确加载并添加项目到我的列表中(每次我到达底部时加载3个项目,依此类推)。 因为我第一次填充列表(前3个项目的第一个加载阶段)时,我无法在空ListView中放置页脚。我决定显示ProgressDialog以便用户知道项目加载。 对于第二个负载阶段,它会加载另外3个项目,以此类推,以便后续加载。此时,ProgressDialog不再可见,而是显示一个页脚,供用户知道项目加载。

尴尬的问题是:当第一次加载时,是的,显示了ProgressDialog。当它第二次加载时,页脚不可见!它没有显示!!!起初我认为在添加页眉和页脚视图之前调用setAdapter是一个问题(我之前正在阅读一些详细说明这个问题的答案)...... 但是没有,这不是我的情况,因为当第三次和随后的时间它加载更多的项目,页脚正确添加并正确显示!当装载完成后,它也被正确删除!关键是,从第三次加载到下一次,一切似乎与页脚及其显示正常工作 ....

这是我认为问题所在的代码片段:

private class UpdateSection extends AsyncTask<String, Void, Integer> {

    private ProgressDialog progressDialog;
    private View listFooter;

    @Override
    protected void onPreExecute() {
        if(currentItems.size() == 0) { // If there's no items (1st loading):
            progressDialog = new ProgressDialog(context);
            progressDialog.setMessage("Loading news...");
            progressDialog.setCancelable(false);
            progressDialog.show();
        } else { // For the other more items (2nd and next loadings):
            listFooter = layoutInflater.inflate(R.layout.list_footer, null);
            lviItemList.addFooterView(listFooter);
        }
    }

    @Override
    protected Integer doInBackground(String... params) {
        // Execute the update of the selected section:
        return getUpdateFromSectionUrl(params[0]);
    }

    @Override
    protected void onPostExecute(Integer result) {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }

        // Here we validate the returned result:
        if(result == 0) { // SUCCESS:
            Toast.makeText(context, "Success on loading more items", Toast.LENGTH_SHORT).show();
        } else if(result == 1) { // NO UPDATES FOUND:
            Toast.makeText(context, "No updates were found", Toast.LENGTH_SHORT).show();
        } else if(result == 2) { // NO CONNECTION:
            Toast.makeText(context, "Connection to internet it is not established", Toast.LENGTH_SHORT).show();
        } else if(result == 3) { // ERROR:
            Toast.makeText(context, "Error while updating news", Toast.LENGTH_SHORT).show();
        }

        MyCustomAdapter myAdapter = new MyCustomAdapter(activity, currentItems, getResources());
        lviItemList.setAdapter(myAdapter);
        isLoadingList = false;
        lviItemList.removeFooterView(listFooter);
    }

我使用班级AsyncTask从互联网上检索数据; 在onPreExecute内,我选择显示ProgressDialog或FooterView; 在doInBackground内部我从服务器进行数据检索,通过params URL并在这里和那里做一些更新(我认为问题不在于方法getUpdateFromSectionUrl,因为它正在工作很好,没有错误); 在onPostExecute里面,我只是分析上面提到的方法返回的给定结果,然后是ListView的适配器设置。

以下是我的变量的简要说明:

listFooter:包含膨胀的xml布局&#34; R.layout.list_footer&#34;我的页脚设计所在的位置; currentItems:它是一个ArrayList,它包含我必须添加到ListView的自定义当前项。哪个被传递给&#34; MyCustomAdapter&#34;的构造函数。 class(这个类完成了设置ListView所需的适配器的所有脏工作); lviItemList:这是发生此问题的ListView ... getUpdateFromSectionUrl:执行数据检索的方法(返回Integer,而不是int); result:包含方法返回的结果&#34; getUpdateFromSectionUrl&#34; myAdapter:来自我的自定义类&#34; MyCustomAdapter&#34;的适配器,它扩展了&#34; BaseAdapter&#34 ;; isLoadingList:一个布尔变量,仅用于控制数据加载......

如您所见,我在添加页脚视图后调用了setAdapter。而且我也知道这段代码很好因为它符合我的要求,但只适用于第三个加载阶段和下一个加载阶段....

我做错了什么或我在代码中缺少什么?任何线索为什么会发生这种情况???

抱歉我的语法错误和/或拼写错误!!!! 如果有人能帮助我解决这个问题,那就太感激了!

问候!

0 个答案:

没有答案