Android Development: ListView Add Emptyview Help Class

Source: Internet
Author: User

In the project development process, will encounter many of the ListView, the ancient method is every encounter a ListView, in the XML file inside write Emptyview, and then add Emptyview, in fact, in many cases, Different ListView Emptyview is the same, for this, write a simple helper class, in the ListView need to add Emptyview, as long as the addition of a line of code can be implemented


The name of the helper class is Emptyviewhelper, and the code is as follows:


public class Emptyviewhelper {private ListView mlistview;private View emptyview;private Context mcontext;private String m Emptytext;private TextView mtextview;public Emptyviewhelper (ListView listview) {Mlistview = Listview;mcontext = Listview.getcontext (); Initemptyview ();} Public Emptyviewhelper (ListView ListView, String text) {Mlistview = Listview;mcontext = Listview.getcontext (); Memptytext = Text;initemptyview ();} private void Initemptyview () {Emptyview = View.inflate (mcontext, R.layout.empty_view, null);((ViewGroup) Mlistview.getparent ()). AddView (Emptyview); Mlistview.setemptyview (Emptyview); if (! Textutils.isempty (Memptytext)) {((TextView) Emptyview.findviewbyid (R.id.textview)). SetText (Memptytext);}}


This help class, get the current ListView, then inflate write the Emptyview in advance, and then set the Emptyview on it.


Then, when a ListView needs to add Emptyview, as long as the following line of code is available

Emptyviewhelper emptyviewhelper = new Emptyviewhelper (Mlistview, "You do not have an order");

The implementation method is quite simple, and this has the following benefits:

1, pre-written good emptyview, each encounter needs to set Emptyview, as long as a line of code can be achieved

2, when the Emptyview need to change, as long as the change of Emptyview, all the Emptyview in the ListView is changed


Since I've used this help class in my project, I've saved a lot of time in development, and now I'm sharing it as a backup for the next development, and I hope it will be a little helpful ....


Android Development: ListView Add Emptyview Help Class

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.