Display string when listview of Android is empty

Source: Internet
Author: User

Reading comments (0)
① When using listView or gridView, when the list is empty, sometimes a special empty view needs to be displayed to prompt the user. In general, If you inherit the ListActivity, as long
<ListView android: id = "@ id/android: list".../>
<TextView android: id = "@ id/android: empty.../>
TextView is automatically displayed when the list is empty.

②. If you inherit the Activity and want to see the above effect, you need to manually
<ListView android: id = "@ + id/list".../>
<TextView android: id = "@ + id/empty".../>

ListView list = (ListView) findViewById (R. id. mylist );
TextView TV = (TextView) findViewById (R. id. myempty );

List. setEmptyView (TV );

Misunderstanding:
SetEmptyView (View) is a misleading function, and sometimes EmptyView may be written in the code, as shown below:

TextView TV = new TextView (this );
TV. setText ("this is a empty view ")
SetEmptyView (TV );
This is not acceptable...

However, I said on a foreigner's Internet that this is feasible. Pay attention to lines 4 and 5:

[Java]
01. TextView emptyView = new TextView (context );
02. emptyView. setLayoutParams (new LayoutParams (LayoutParams. FILL_PARENT, LayoutParams. FILL_PARENT ));
03. emptyView. setText ("This appears when the list is empty ");
04. emptyView. setVisibility (View. GONE );
05. (ViewGroup) list. getParent (). addView (emptyView );
06. list. setEmptyView (emptyView );
TextView emptyView = new TextView (context );
EmptyView. setLayoutParams (new LayoutParams (LayoutParams. FILL_PARENT, LayoutParams. FILL_PARENT ));
EmptyView. setText ("This appears when the list is empty ");
EmptyView. setVisibility (View. GONE );
(ViewGroup) list. getParent (). addView (emptyView );
List. setEmptyView (emptyView );
 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.