单击下一步按钮后,如何将 ListView 中显示的列表项限制为 10 和接下来的 10 [英] How to limit list items display in ListView by 10 and next 10after clicking next button

查看:19
本文介绍了单击下一步按钮后,如何将 ListView 中显示的列表项限制为 10 和接下来的 10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 100 个项目的列表视图,我想显示前 10 个项目,单击下一个按钮时我必须显示下一个 10 个项目,即从 11 到 20,我有获取前 10 个项目的代码

i have a listview with 100 items and i want to display first 10 items and on click of next button i have to display next 10 ie.,from 11 to 20, i have the code for getting first 10 items

public int getCount() {
        return 10;
    }

但是如何单独获得接下来的 10 件物品等等.任何想法

but how to get next 10 items alone and so on. any idea

推荐答案

一种方法是,

1) 不要用所有数据填充您的 ArrayList.而是将它们保存在单独的 ArrayList (al1) 中,并使用最多 10 个值的 ArrayList (al2) 与 BaseAdapter 一起使用.

1) Don't populate your ArrayList with all the data. Instead keep them in a separate ArrayList (al1) and use an ArrayList (al2) with max 10 values to use with your BaseAdapter.

2) 最初,

  • al2 = al1[0] 到 al1[9]
  • BaseAdatper(上下文,数据)

3) 保持 BaseAdapter 不变,但要改变

3) Keep the BaseAdapter as it is but change

 @Override
 public int getCount() {
 return 10;
}

 @Override
 public int getCount() {
 return data.size();
}

这不是必须的,但这是一种很好的做法.现在您将只显示 10 个项目,因为这就是您传递给适配器的全部内容.还要在扩展的 BaseAdatper 类中编写一个公共函数来设置 data 变量.

It's not a must but it's good practice. Now you'll be showing only 10 items cause that's all you are passing to the adapter. Also write a public function in your extended BaseAdatper class to set the data variable.

4) 在下一个按钮单击事件中,从 al1 中获取接下来的 10 个项目并分配给 al2.使用您编写的公共函数用 al2 覆盖 data.

4) On the next button click event get the next 10 items from al1 and assign to al2. Use the public function you wrote to over write data with al2.

5) BaseAdapter 有一个方法叫做 notifyDataSetChanged ,叫它.这样做是告诉适配器从上到下刷新.由于您在刷新时用新数据覆盖了 data,您将看到新数据.就是这样.

5) BaseAdapter has a method called notifyDataSetChanged , call it. What this does is tell the adapter to refresh from top to bottom. Since you have data over written with new data when the refresh occurs you'll be seeing the new data. That's it.

我认为您想出一种方法来跟踪您当前显示的 al1 中的哪个索引到哪个索引并不困难.:)

I don't think it'll be difficult for you to come up with a way to keep track from which index to which in al1 you're currently displaying. :)

这篇关于单击下一步按钮后,如何将 ListView 中显示的列表项限制为 10 和接下来的 10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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