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 8375897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:17:37+00:00 2026-06-09T15:17:37+00:00

How can I get the current Android view when it displays data that has

  • 0

How can I get the current Android view when it displays data that has been updated, and force it to be redrawn? I worked through Android’s Notepad tutorial and completed lesson three without any problems — the solution is provided, after all — but I’m stuck on my first non-trivial modification.

I added a new button to the menu, next to the Add note button. When pressed, that button adds a letter to the title of each note in the system. However, the new titles don’t show up in the list of notes no matter how long I wait. I know the updater works because the changes do appear if I dismiss the app and bring it back up.

So far, I’ve discovered that I have to use some kind of invalidation method to make the program redraw itself with the new values. I know that invalidate() is used from the UI thread and postInvalidate() is used from non-UI threads 1, 2, but I don’t even know which thread I’m in. Also, both of those methods have to be called from the View object that needs drawing, and I don’t know how to obtain that object. Everything I try returns null.

My main class:

public boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch(item.getItemId()) {
        case INSERT_ID:
            createNote();
            return true;
        case NEW_BUTTON:
            expandTitles();
            return true;
        default:
            // Intentionally empty
    }
    return super.onMenuItemSelected(featureId, item);
}

private void expandTitles() {
    View noteListView = null;

    // noteListView = findViewById(R.layout.notes_list); // null

    // noteListView =
    //   getWindow().getDecorView().findViewById(android.R.id.content);
    // From SO question 4486034

    noteListView = findViewById(R.id.body); // Fails

    mDbHelper.expandNoteTitles(noteListView);
}

My DAO class:

public void expandNoteTitles(View noteListView) {
    Cursor notes = fetchAllNotes();
    for(int i = 1; i <= notes.getCount(); i++) {
        expandNoteTitle(i);
    }

    // NPE here when attempt to redraw is not commented out
    noteListView.invalidate(); // Analogous to AWT's repaint(). Not working.
    // noteListView.postInvalidate(); // Like repaint(). Not working.
}

public void expandNoteTitle(int i) {
    Cursor note = fetchNote(i);
    long rowId =
      note.getLong(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_ROWID));
    String title =
      note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)) + "W";
    String body =
      note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY));
    updateNote(rowId, title, body);
}

What do I have to do to get the updated note titles to show up as soon as I press the button?

Obviously, I’m a complete newbie to Android. I point this out to encourage you to use small words and explain even obvious things. I know this is the millionth “Android not redrawing” question, but I’ve read dozens of existing posts and they either don’t apply or don’t make sense to me.

1: What does postInvalidate() do?
2: What is the difference between Android's invalidate() and postInvalidate() methods?

  • 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-09T15:17:38+00:00Added an answer on June 9, 2026 at 3:17 pm

    According to the tutorial, the list of existing notes are presented in a ListView. That is an adapter based View, so the items it shows are sourced from an adapter extending theBaseAdapter class. In these cases, you should notify the adapter that the contents have changed by calling its notifyDatasetChanged method. This’ll signal the ListView to update and redraw its rows.

    Edit:

    Sorry, I now realize that this example uses CursorAdapters. These source the items to show from a Cursor object that was obtained from a database query. Now, what the notifyDatasetChanged() tells the adapter is, that the data that backs the adapter has changed, so Views that show stuff based on this adapter need to redraw their contents. In the case of a CursorAdapter, this data is coming from a cursor. So you also need to requery that cursor, refreshing it from the DB, like this:

    private void expandTitles() {
            mDbHelper.expandNoteTitles();
    
            CursorAdapter adapter = (CursorAdapter)getListAdapter();
            adapter.getCursor().requery();
        }
    

    The requery() method automatically calls the notifyDatasetChanged() in this case, so you don’t need to worry about that, the list will update itself. See this thread also: https://groups.google.com/forum/?fromgroups#!topic/android-developers/_FrDcy0KC-w%5B1-25%5D.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can i get the current view criteria using php and the mysql statement
Is there any Android class/ api by which i can get current radio frequency
How can I get the current Android SDK version(1.5, 1.6, 2.0, etc.) programmatically?
How can i get the current android deveice version (1.5, 1.6, 2.0, etc.) programmatically.
How many ways I can get the current geo location (lat,lang) programatically in Android,
The Android documentation here http://developer.android.com/guide/topics/resources/localization.html explains that you can get the current locale with
How can I get the package name of the current launcher in android 2.3
I can get the current selected row in this way: private void DataGridView1_CellContentClick(object sender,
How can I get current time (I need hours, minutes, seconds) crossplatform in c++?
In C# I can get the current user of a web app using the

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.