计数的ListView项错误 [英] Counting listView items error
本文介绍了计数的ListView项错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图来算的ListView项目。使用im这个code:
I am trying to count listView items. Im using this code:
int count=0;
ListView listView = (ListView) findViewById(R.id.listView1);
for(int i = 0; i <= listView.getLastVisiblePosition(); i++)
{
if(listView.getChildAt(i)!= null)
{
count++;
}
}
Toast.makeText(getApplicationContext(), String.valueOf(count), Toast.LENGTH_SHORT).show();
为什么COUNT变量值始终为0,当ListView中显示一些记录?
Why the COUNT variable value is always 0, when listView display some records?
推荐答案
如果您正在寻找所有的ListView项目的数量,你可以使用这个调用(请确保适配器的设置):
If you are looking for count of all ListView items, you can use this call (make sure adapter is set):
listView.getCount();
如果你想要的是可见的项目数,试试这个(仅适用于可见的ListView):
If what you want is count of visible items, try this (works only for visible ListView):
listView.getLastVisiblePosition()-listView.getFirstVisiblePosition();
这篇关于计数的ListView项错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文