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();
Why the COUNT variable value is always 0, when listView display some records?
Use this .It helps mine
If you are looking for count of all ListView items, you can use this call (make sure adapter is set):
If what you want is count of visible items, try this (works only for visible ListView):
Let me explain the reason.. You've just get the listview like this
so listview has no elements and then you are tring to get the last visible postion by using
listView.getLastVisiblePosition()
it always returns zero because your listview hasn't yet bind with any adapter, i.e your listview is empty at the time your are getting the last visible position try toplace this code after binding Adapter to the listview