在页面上动态增长ListView Android App

时间:2014-09-04 19:20:50

标签: java android listview

所以,我有一个listview。它从服务器一次加载10行。当用户滚动到底部时,它将再加载10个。当没有更多行要添加时,我希望它显示一个“结果结束”的页脚视图。因此,当我设置适配器时,我无法添加footerview,因为它会在前10行之后显示“结果结束”,并且可能会添加更多结果。这是我的代码:

这是我尝试过的,没有错误但是没有显示footerview。

        if (finished == 1) {
            Log.d("Test", "at the end of listview");
            footerView = getLayoutInflater().inflate(
                R.layout.listview_footer, listView, false);
            listView.addFooterView(footerView);
        }
记录列表视图末尾的

和测试,所以我知道它在最后。有任何想法吗?感谢。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    arrayOfLocations = new ArrayList<Location>();

    startLoop = 0;
    endLoop = 10;

    listView = (ListView) findViewById(R.id.listView1);

    adapter = new LocationAdapter(this, arrayOfLocations);

    FillLocations myFill = new FillLocations();
    myFill.execute();

    listView.setAdapter(adapter);
    listView.setOnScrollListener(new OnScrollListener() {
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {

            // if user scrolls...
            if (listView != null
                    && listView.getAdapter() != null
                    && listView.getChildAt(listView.getChildCount() - 1) != null
                    && listView.getLastVisiblePosition() == listView
                            .getAdapter().getCount() - 1
                    && listView.getChildAt(listView.getChildCount() - 1)
                            .getBottom() <= listView.getHeight()
                    && finished == 0) {
                startLoop += 10;
                endLoop += 10;
                FillLocations myList = new FillLocations();
                myList.execute();
            }
        }
    });
}

private class FillLocations extends
        AsyncTask<Integer, Void, ArrayList<Location>> {

    int finished = 0;

    // Decode image in background.
    @Override
    protected ArrayList<Location> doInBackground(Integer... params) {

                ...

        // parse json data
        try {
            JSONArray jArray = new JSONArray(result);
            // if there are less than 10 listings left (no more results)
            if (jArray.length() < endLoop) {
                endLoop = jArray.length();
                finished = 1;
            }

            for (int i = startLoop; i < endLoop; i++) {
                final JSONObject json = jArray.getJSONObject(i);
                // add rows to adapter
            }

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return arrayOfLocs;
    }

    protected void onPostExecute(ArrayList<Location> result) {

        //if it is at the end, add the footer
        if (finished == 1) {
            Log.d("End", "at the end");
            footerView = getLayoutInflater().inflate(
                R.layout.listview_footer, listView, false);
            listView.addFooterView(footerView);
        }
        for (Location location : result)
            adapter.add(location);

    }
}

1 个答案:

答案 0 :(得分:0)

您将footerview添加到listview但不刷新适配器。所以在添加listview之后,您必须通知以下列表视图:

adapter.notifyDataSetChanged();

所以你的代码必须是这样的:

if (finished == 1) {
        Log.d("Test", "at the end of listview");
        footerView = getLayoutInflater().inflate(
            R.layout.listview_footer, listView, false);
        listView.addFooterView(footerView);
        adapter.notifyDataSetChanged();
    }

编辑:

在onPostExecute()

上试试
protected void onPostExecute(ArrayList<Location> result) {

     arrayOfLocations.addAll(result);
     if (finished == 1) {
        Log.d("End", "at the end");
        footerView = getLayoutInflater().inflate(
            R.layout.listview_footer, listView, false);
        listView.addFooterView(footerView);
    }
    adapter = new LocationAdapter(context, arrayOfLocations);
    listview.setAdapter(adapter);

}

在onCreate()

上删除此行
listView.setAdapter(adapter);