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.
How can I fix this problem?
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 themy_array.clear();
then add data to it and notify the Listview adaapter.Example Code:
EDIT
For your undetected Button Clicks problem inside the list view.
Just set onClick attribute in the custom listview's layout file.
and in your onClick method, do the Implementation of click events.
P.S: you need to set the ListView object as a class variable.