3

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Araz Jafaripur
  • 927
  • 2
  • 12
  • 32
  • Did you call `notifyDataSetChanged` on the listview after loading new data? – kabuto178 Jul 20 '13 at 13:48
  • No, I Remove my array item and again fill array and set listview adapter . you thin i get search query from database, must to delete before data and get new data . – Araz Jafaripur Jul 20 '13 at 13:56

1 Answers1

3

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 the my_array.clear(); then add data to it and notify the Listview adaapter.

Example Code:

Class myclass extends Activity
{
 private ListviewAdapter adapter;
 private ArrayList<String> my_array;

 onCreate(...)
  {
   //Fill Values in the Array
   my_array.add(...);
   adapter = new ListviewAdapter(MainActivity.this, my_array);
   list.setAdapter(adapter);
   ....

   //In your onScrollListener() of the list make the following changes
   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)
            {
                my_array.clear();
                //Add the values in you Array here.
                my_array.add(....)
                //Notify the adapter about the data set change.
                adapter.notifyDatasetChanged();
            }
        }
    }
});

EDIT
For your undetected Button Clicks problem inside the list view.
Just set onClick attribute in the custom listview's layout file.

<Button
        ......
        android:onClick="btnRemoveClick"
        />

and in your onClick method, do the Implementation of click events.

public void btnRemoveClick(View v)
{
    //get the position of the button here
    final int position = listviewItem.getPositionForView((View) v.getParent()); 
}

P.S: you need to set the ListView object as a class variable.

Salman Khakwani
  • 6,684
  • 7
  • 33
  • 58
  • this is work, but have one problem!
    when editbox is filled to search i can't lontouch listview to select or can't click on image button in inside of listview in custom layout, but when editbox is empty all function like on longtouch or clickable imagebutton for each row .
    – Araz Jafaripur Jul 20 '13 at 16:44
  • It took me a while to understand what you meant, never mind. Here is the solution to your other problem. http://stackoverflow.com/questions/7763983/how-to-do-button-click-and-list-view-item-click-in-customize-list-view-in-androi – Salman Khakwani Jul 20 '13 at 18:08
  • Before i test this, don't work for me . your code for me work nicly but when i use adapter.notify... i can't to select a row of listview or longtouch listview item, this problem occure when i type in edittext to search when i clear edittext text i cant select row or longtouch ! – Araz Jafaripur Jul 20 '13 at 18:21
  • 1
    wrong from me, i don't know when use notify scroll position save automaticly, i remove this line want to save position Parcelable state = list.onSaveInstanceState(); and this restore scroll code list.onRestoreInstanceState(state); thank . – Araz Jafaripur Jul 20 '13 at 19:12