How to display a "Loading..." text while retrieving items for a ListView

15,486

Solution 1

When user scrolls to the bottom your adapter's getView should return a view with "Loading.." text and spinning progress. At the same time AsyncTask or background thread should be started that downloads a new chunk of content. When content is ready adapter.notifyDatasetChanged() is called and ListView redisplays all content including the new items.

So "Loading..." message is just a last item in the ListView.

Solution 2

That's done with the help of AsyncTask (an intelligent backround thread) and ProgressDialog

When the AsyncTask starts we reaise a progressdialog with indeterminate state, once the task is finished we dismiss the dialog.

Example code
What the adapter does in this example is not important, more important to understand that you need to use AsyncTask to display a dialog for the progress.

private class PrepareAdapter1 extends AsyncTask<Void,Void,ContactsListCursorAdapter > {
    ProgressDialog dialog;
    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(viewContacts.this);
        dialog.setMessage(getString(R.string.please_wait_while_loading));
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        dialog.show();
    }
    /* (non-Javadoc)
     * @see android.os.AsyncTask#doInBackground(Params[])
     */
    @Override
    protected ContactsListCursorAdapter doInBackground(Void... params) {
        cur1 = objItem.getContacts();
        startManagingCursor(cur1);

        adapter1 = new ContactsListCursorAdapter (viewContacts.this,
                R.layout.contact_for_listitem, cur1, new String[] {}, new int[] {});

        return adapter1;
    }

    protected void onPostExecute(ContactsListCursorAdapter result) {
        list.setAdapter(result);
        dialog.dismiss();
    }
}

Solution 3

I don't know whether it will completely fits your requirement, simple way is just set the loading text initially to android-empty-id-view like

 <TextView
        android:id="@android:id/empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Loading... Please Wait"
        android:textSize="20dip" />

and if no list items found then set android-empty-id-view to different message in async callback

((TextView) findViewById(android.R.id.empty)).setText("Your List is empty");

it will give enough indication to user that something is progressing..i.e list is loading

Share:
15,486
ggomeze
Author by

ggomeze

Telecommunications Engineer developing software. Specialist in Mobile Development with Android and iOS (Objective-C and Swift). Also experience with complete Web Development solutions: Ruby on Rails, HTML5, JS, CSS3

Updated on June 20, 2022

Comments

  • ggomeze
    ggomeze about 2 years

    There are some others applications doing this, like Twitter, Facebook, or even native applications such as Android Market. When you want to display a list of items retrieved from the internet, this looks like a standard way for displaying the user some notification about action in progress. This is a white background screen with an animated spinning wheel and a "Loading..." text.

    Does somebody know how to do this?

    I've been able to do something similar with this code, but I don't like it too much yet. Still work in progress:

    <ListView android:id="@+id/post_list" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    
       <TextView android:id="@android:id/loading" 
           android:background="@color/white"
           android:layout_width="fill_parent"     
           android:layout_height="fill_parent" 
           android:gravity="center" android:text="Loading..." />
    
  • ggomeze
    ggomeze almost 14 years
    Ops, or i didn't explain myself properly, or i'm not sure i understand your answer. I don't want to display a dialog, but a textview, like it's displayed on Android Market as an example. Also Twitter, or Facebook use same TextView. Have you seen that with screen with the loading... text view centered on it?. Thanks for the reply though
  • Pentium10
    Pentium10 almost 14 years
    Sorry, this god me confused This is a white background screen with an animated spinning wheel and a "Loading..."
  • ggomeze
    ggomeze almost 14 years
    Hey Fedor! (aka "the man who solved the OOM error"). I kind of like more your answer. Do you have a snippet for that?. Otherwise i think i can figure it out...
  • Fedor
    Fedor almost 14 years
    Sorry, don't have snippet. I just use this approach in my projects and it works fine. I guess Market does something like that.
  • ggomeze
    ggomeze almost 14 years
    I've found interesting the setEmptyView method of the ListView. There you can add the process bar widget with a text. I haven't made it work perfectly yet... :-(
  • Vikas
    Vikas about 13 years
    I've followed your instructions and implemented it in my application too.. thanks.. People can see my post here.
  • Maxrunner
    Maxrunner about 12 years
    how do you separate this view with the rest of elements in getview method?
  • roxrook
    roxrook over 11 years
    From getView() method, how do you find out which one is the last item?