The problem I have is that listView.getLastVisiblePosition
always returns -1 so I can't hide the searchView
. I check this right after setting the adapter
and anywhere I have tried to put this it still returns -1. I didn't see in the Docs why this would be but I imagine it would return -1 if the ListView
is not showing any items. However, listView.getFirstVisiblePosition()
returns 0 always, even when there is more than one item showing.
I have tried both methods Here but it doesn't make a difference when getting the wrong value.
@SuppressLint("NewApi") private void setFilters(String curType, Object curFilter)
{
// initialize several lists
itemsAdapter = new ArrayAdapter<Rowdata>(this, R.layout.list_item_text, foodItems);
listView.setAdapter(itemsAdapter);
int numItems = listView.getLastVisiblePosition() - listView.getFirstVisiblePosition();
if (numItems > foodItems.length)
{ searchField.setVisibility(View.GONE); }
else
{ searchField.setVisibility(View.VISIBLE); }
}
This method is called any time a Button
is pressed or text is changed that can filter through the list. So the question is why would listView.getLastVisiblePosition()
always return -1 and why would listView.getFirstVisiblePosition()
always return 0? No errors/exceptions, everything runs fine except for not getting the expected results. Note: itemsAdapter.getCount()
returns the correct value.
Also, I have to support API >=10
Edit
If anyone needs clarification, let me know. But basically, I have an EditText
I use to search through the list. I want to hide this when there aren't more items in the list than what fit on the screen. listView.getLastVisiblePosition()
always returns -1
I would really like to know the cause of the original problem but if anyone has any better way of hiding the search box when items all fit on the screen, I am open to suggestions.
Update
I put a breakpoint in onItemClick()
and there I get the correct values for getFirstVisiblePosition()
, getLastVisiblePosition()
, and listView.getChildCount()
. Before this, I get 0, -1, and null
respectively.