Java 将页脚放在动态增长的ListView Android应用程序上

Java 将页脚放在动态增长的ListView Android应用程序上,java,android,listview,Java,Android,Listview,所以,我有一个列表视图。它一次从服务器加载10行。当用户滚动到底部时,它将再加载10个。当没有更多的行要添加时,我希望它显示一个页脚视图,上面写着“结果结束”。因此,在设置适配器时,我无法正确添加footerview,因为在前10行之后,它会显示“结果结束”,可能还有更多的结果要添加。这是我的密码: 这是我尝试过的,没有错误,但footerview不显示 if (finished == 1) { Log.d("Test", "at the end of l

所以,我有一个列表视图。它一次从服务器加载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);
        }
listview末尾的测试被记录下来,所以我知道它在末尾。有什么想法吗?谢谢

@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);

    }
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayOfLocations=新的ArrayList();
startoop=0;
endLoop=10;
listView=(listView)findViewById(R.id.listView1);
适配器=新位置适配器(此为ArrayFlocations);
FillLocations myFill=新的FillLocations();
myFill.execute();
setAdapter(适配器);
setOnScrollListener(新的OnScrollListener(){
@凌驾
public void onScroll(AbsListView视图,int firstVisibleItem,
int visibleItemCount,int totalItemCount){
//如果用户滚动。。。
如果(listView!=null
&&listView.getAdapter()!=null
&&listView.getChildAt(listView.getChildCount()-1)!=null
&&listView.getLastVisiblePosition()=listView
.getAdapter().getCount()-1
&&listView.getChildAt(listView.getChildCount()-1)

.getBottom()您将footerview添加到listview,但不刷新适配器。因此,在添加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()上尝试此操作


谢谢你的回答,但不幸的是,我已经尝试过了,如果它是真的,它仍然不起作用,它是记录“在末尾…”吗?是的,它确实在末尾记录。而且,当我添加footerview时,适配器没有更改,所以调用adapter.notifyDataSetChanged();基本上是无用的,对吗?
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);

}
listView.setAdapter(adapter);