Android中大数据加载中的分页listview [英] Pagination listview in large data loading in Android

查看:430
本文介绍了Android中大数据加载中的分页listview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,并正在将数据库中的数据加载到列表视图中.在用户将列表视图滚动到末尾以从数据库中获取另外20个项目并将其添加到列表视图之后,我写了从数据库中获取20个项目的信息,但是在加载到列表视图时加载一个或两个项目存在问题.我在列表视图中看不到任何数据.我的代码是:

I have a database and am loading data from that to a listview. I write to get 20 items from the database after the user scrolls the listview to the end to get another 20 items from the database and add to the listview, but there is a problem on loadinging one or two items in loading to the listview; I can't see any data in the listview. My code is:

private ListviewAdapter adapter;
list.setOnScrollListener(new OnScrollListener() {
    @Override
    public void onScrollStateChanged(AbsListView arg0, int arg1) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, final int totalItemCount) {
        if (totalItemCount > 0)
        {
            int lastInScreen = firstVisibleItem + visibleItemCount;
            if(lastInScreen == totalItemCount)
            {
                Parcelable state = list.onSaveInstanceState();

                //Fill my_array
                adapter = new ListviewAdapter(MainActivity.this, my_array);
                list.setAdapter(adapter);
                list.onRestoreInstanceState(state);
            }
        }
    }
});

当加载的数据大于屏幕时,它可以正常工作,但是当加载的数据小于屏幕时,列表视图中什么都看不到.

When the data loaded is larger than the screen, it works fine, but when the loaded data is smaller than the screen, I don't see anything in the listview.

如何解决此问题?

推荐答案

只需将适配器作为类变量并在onCreate(..)中设置适配器 在到达ListView的末尾并更新列表视图时,只需清除my_array.clear();,然后向其中添加数据并通知Listview适配器.

Just take the adapter as a class variable and set the adapter in onCreate(..) and when you are reaching the end of ListView and updating the list view, just clear the my_array.clear(); then add data to it and notify the Listview adaapter.

示例代码:

Class myclass extends Activity
{
 private ListviewAdapter adapter;
 private ArrayList<String> my_array;

 onCreate(...)
  {
   //Fill Values in the Array
   my_array.add(...);
   adapter = new ListviewAdapter(MainActivity.this, my_array);
   list.setAdapter(adapter);
   ....

   //In your onScrollListener() of the list make the following changes
   list.setOnScrollListener(new OnScrollListener() {
    @Override
    public void onScrollStateChanged(AbsListView arg0, int arg1) {

    }
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, final int totalItemCount) {
        if (totalItemCount > 0)
        {
            int lastInScreen = firstVisibleItem + visibleItemCount;
            if(lastInScreen == totalItemCount)
            {
                my_array.clear();
                //Add the values in you Array here.
                my_array.add(....)
                //Notify the adapter about the data set change.
                adapter.notifyDatasetChanged();
            }
        }
    }
});

编辑
对于列表视图内未检测到的按钮单击"问题.
只需在自定义列表视图的布局文件中设置onClick属性即可.

EDIT
For your undetected Button Clicks problem inside the list view.
Just set onClick attribute in the custom listview's layout file.

<Button
        ......
        android:onClick="btnRemoveClick"
        />

并在您的onClick方法中,执行点击事件.

and in your onClick method, do the Implementation of click events.

public void btnRemoveClick(View v)
{
    //get the position of the button here
    final int position = listviewItem.getPositionForView((View) v.getParent()); 
}

P.S:您需要将ListView对象设置为类变量.

P.S: you need to set the ListView object as a class variable.

这篇关于Android中大数据加载中的分页listview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆