Mainak Mukherjee
Mainak Mukherjee

Reputation: 782

Listview is automatically scrolling to the top

I have a listview in my project,it contain image and info.
SO assume it contain 50 persons info. Now if i want to delete the last two item,after deleting the last item the listlview is automatically focusing to the top of the list.
So now the user have scroll down all the item to reach to the 49th item and to delete it.
So is there any way to fix the listview postion at that position from where i am deleting the item.

//code for delete info

public void deleteFeed(int rowID) {
        // fetch the image File name
        Cursor cursor = dbAdapter.getTimelineFeedByID(mSysPrefs.getBabyID(), rowID);
        if(cursor != null && cursor.moveToFirst()) {
            // fetch from DB Cursor
            String imageFileName = cursor.getString(cursor.getColumnIndex(DatabaseAppHelper.KEY_IMAGENAME));
            File file = new File(GlobalConstants.FILE_PATH, imageFileName + GlobalConstants.IMAGE_EXTENSION);
            if(file.exists()) {
                file.delete();
            }
            //listview.smoothScrollToPosition(0, listview.getHeight());
            listview.smoothScrollToPosition(0, listview.getBottom());
        }
        // now delete the record from the DB    
        if(dbAdapter.deleteTimelineFeed(rowID) != -1) {
            onResume();
        } else {
            Toast.makeText(this, "Please try again..", Toast.LENGTH_LONG).show();
        }
    }

//code for listview creation

@Override
protected void onResume() {
    super.onResume();
     turnGPSOn(); // method to turn on the GPS if its in off state.
     getMyCurrentLocation();  
    dbAdapter.openDBConnection();
    new Handler().post(new Runnable() {
        @Override
        public void run() {
            timelineCursorAdapter = new TimelineCursorAdapter(TimelineViewActivity.this,
                    dbAdapter.getAllTimelineFeedsForBaby(mSysPrefs.getBabyID()), 0);
            listview.setAdapter(timelineCursorAdapter);
            //timelineCursorAdapter.notifyDataSetChanged();
            listview.invalidateViews();

        }
    });
}

Upvotes: 2

Views: 750

Answers (3)

TechInsect
TechInsect

Reputation: 124

getCount() returns the current item position which the adapter holds. Following method can help by calling it in onResume()

mListView.smoothScrollToPosition(mAdapter.getCount());

Upvotes: 0

rams
rams

Reputation: 1580

Get the position from you deleted item and then pass the position to setselection method like this: listview.setSelection(pos);

Upvotes: 1

Ramesh
Ramesh

Reputation: 526

Try with below code

Before deleting list items get visible position by using below code

       mIndex = listView.getFirstVisiblePosition();
        View v = listView.getChildAt(0);
        mTop = (v == null) ? 0 : v.getTop();

After deleting items set that position to list view

    listView.setSelectionFromTop(mIndex, mTop);

Upvotes: 0

Related Questions