Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7850767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:57:56+00:00 2026-06-02T18:57:56+00:00

I am trying to create a listview that when you click on an entry

  • 0

I am trying to create a listview that when you click on an entry you may modify it and after hitting confirm it will still show the entry you just modified instead of scrolling to the top.

made advised changes and after leaving the edit activity it still scrolls to the top and does not find the correct scroll position. am i missing something?

static int firstPosition = 0;

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Cursor c = mNotesCursor;
    ListView mListView = getListView();
    firstPosition = mListView.getFirstVisiblePosition();
    c.moveToPosition(position);
    Intent i = new Intent(this, QuoteEdit.class);
    i.putExtra(QuotesDBAdapter.KEY_ROWID, id);
    i.putExtra(QuotesDBAdapter.KEY_QUOTES, c.getString(
    c.getColumnIndexOrThrow(QuotesDBAdapter.KEY_QUOTES)));
    startActivityForResult(i, ACTIVITY_EDIT);
}
    public void onResume(int requestCode, int resultCode, Intent intent) {
ListView mListView = getListView();

if (mListView != null && firstPosition >= 0){
   mListView.scrollTo(0,firstPosition);
//  mListView.setSelection(firstPosition);

}

}
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    try {
        super.onActivityResult(requestCode, resultCode, intent);
        Bundle extras = intent.getExtras();
        switch(requestCode) {
            case ACTIVITY_CREATE:
                String title = extras.getString(QuotesDBAdapter.KEY_QUOTES);
                mDbHelper.createQuote(title);
                break;
            case ACTIVITY_EDIT: 
                ListView mListView = getListView();
                Long rowId = extras.getLong(QuotesDBAdapter.KEY_ROWID);
                if (rowId != null) {
                    String editTitle = extras.getString(QuotesDBAdapter.KEY_QUOTES);
                    mDbHelper.updateQuote(rowId, editTitle);
                    mListView.setSelection(firstPosition);
                }
                fillData();
                break;
        }
    }
    catch (Exception ex){
        Context context = getApplicationContext();
        CharSequence text = ex.toString();
        int duration = Toast.LENGTH_LONG;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:57:57+00:00Added an answer on June 2, 2026 at 6:57 pm

    Your editor is another Activity, which you are calling with startActivityForResult(). This line doesn’t actually stop here – it will continue processing the rest of the method, including the mListView.setSelection(firstPosition); statement. In other words, setSelection() is called before the user gets to edit the value.

    When the editor Activity is closed, it will return the result to onActivityResult(). In this method, you will need to tell it to scroll the ListView to the correct position again. So maybe something like this…

    static int firstPosition = 0; // this is a global variable - set it in onListItemClick()
    
    public void onActivityResult (int requestCode, int resultCode, Intent data){
        mListView.setSelection(firstPosition);
        }
    

    And change your onListItemClick() method to use the global variable, like this…

    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Cursor c = mNotesCursor;
        ListView mListView = getListView();
        firstPosition = mListView.getFirstVisiblePosition(); // changed this line
        c.moveToPosition(position);
        Intent i = new Intent(this, QuoteEdit.class);
        i.putExtra(QuotesDBAdapter.KEY_ROWID, id);
        i.putExtra(QuotesDBAdapter.KEY_QUOTES, c.getString(
        c.getColumnIndexOrThrow(QuotesDBAdapter.KEY_QUOTES)));
        startActivityForResult(i, ACTIVITY_EDIT);
        mListView.setSelection(firstPosition);
    }
    

    Now, the setSelection() method doesn’t change the scroll position, but you could try this…

    In onListItemClick(), change to the following…

        firstPosition = mListView.getScrollY();
    

    And in onActivityResult(), change to the following…

        mlistView.scrollTo(0,firstPosition);
    

    Possibly also add the following code to the end of your onResume() method (in your main Activity), so that it is run when you return back to the main Activity…

    ListView mListView = getElementById("myListView");
    if (mlistView != null && firstPosition >= 0){
        mlistView.scrollTo(0,firstPosition);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently trying to create a program that will show a slideshow of
I am trying to create a ListView that will be populated with the entries
I'm trying to create an AlertDialog that opens when an item is click in
I'm trying to create a context menu, so that when you right click the
So I'm trying to create a screen which has a ListView and over that
I'm trying to create a UserControl that contains a ListView and a number of
I'm trying to register a long click on my listView row so it will
I am trying to create a ListView with CheckBox's...Th ListView should allow the user
I am trying to create an interactive ListView without extending the ListActivity class.The layout
I'm trying to create a table with Listview and one of the fields I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.