Solution to the Android ListView data null and Load Error method _android

Source: Internet
Author: User
Tags stub

In your project, list controls such as ListView or GridView are used. Typically used to show data requested from the network. If the requested data is empty or if there is no network at the time of the request, how should our interface be displayed? When the data is empty, ListView can use the Setemptyview (View Emptyview) method to the unified interface we need. Data loading failed? We can also deal with it uniformly.

The following class is simply encapsulated for a page with no data and load errors. public class Commonshowview {Private contextual mcontext;//context private ViewGroup memptyorerrorview;//page load no data or load error display P Rivate ViewGroup mcontentview;//The content displayed when the load succeeds private viewgroup mparentview;//parent layout viewgroup Private Layoutinflater MInflat
 Er
 Private TextView no_net;
 Private Button load_btn;
 Private Boolean mviewsadded; Private Viewgroup.layoutparams mlayoutparams = new Viewgroup.layoutparams (layoutparams.match_parent,
 Layoutparams.match_parent); Public final static int type_empty = 1;//data is empty public final static int type_error = 2;//Loading data failed public final static int Type_content = 3;//Direct display content private int mtype = type_empty;//data type, default is no data/** * Construction method, incoming context and content Groupview/public Co
  Mmonshowview (context context, ViewGroup Mcontentview) {mcontext = context;
  Minflater = Layoutinflater.from (Mcontext);
  This.mcontentview = Mcontentview;
  Mparentview = (viewgroup) mcontentview.getparent ();
 Initviews ();
} @SuppressLint ("Inflateparams") private void Initviews () {Memptyorerrorview = (viewgroup) minflater.inflate (r.layout.common_show, NULL);
  No_net = (TextView) Memptyorerrorview.findviewbyid (r.id.no_net);
  LOAD_BTN = (Button) Memptyorerrorview.findviewbyid (R.ID.LOAD_BTN);
   if (!mviewsadded) {mviewsadded = true;
  Mparentview.addview (Memptyorerrorview, mlayoutparams);
    Load_btn.setonclicklistener (New Onclicklistener () {//Check the network for network settings @Override public void OnClick (View v) {
    Intent Intent = null; To determine the version of the mobile phone system, API greater than 10 is 3.0 or more version and the Phantom of the mobile phone if (Android.os.Build.VERSION.SDK_INT > &&!android.os.build.manufa
    Cturer.equals ("Meizu")) {intent = new intent (Android.provider.Settings.ACTION_WIRELESS_SETTINGS); else if (Android.os.Build.VERSION.SDK_INT > && android.os.Build.MANUFACTURER.equals ("Meizu")) {I
    ntent = new Intent (Android.provider.Settings.ACTION_SETTINGS);
     else {intent = new intent (); ComponentName component = new ComponeNtname ("Com.android.settings", "com.android.settings.WirelessSettings");
     Intent.setcomponent (component);
    Intent.setaction ("Android.intent.action.VIEW");

   } mcontext.startactivity (Intent);
 }
  });
 Public ViewGroup Getemptyorerrorview () {return memptyorerrorview; /** * Set no data or load Error view * @description: * * public void Setemptyorerrorview (ViewGroup emptyorerrorview) {THIS.MP
  Arentview.removeview (This.memptyorerrorview);
  This.mParentView.addView (Emptyorerrorview, mlayoutparams);
 This.memptyorerrorview = Emptyorerrorview;  /** * Set no data or load Error view * @description: */public void Setemptyorerrorview (int res) {ViewGroup VG = (viewgroup)
  Minflater.inflate (res, NULL);
  This.mParentView.removeView (This.memptyorerrorview);
  This.mParentView.addView (VG, mlayoutparams);
 This.memptyorerrorview = VG;
 /** * Get content View * @description: * * public viewgroup Getcontextview () {return mcontentview; /** * Method Overview: Get the parent layout of the content view * * PubliC ViewGroup Getrootview () {return mparentview;
  /** * Method Overview: Set the view of the display/public void Setcontextview (ViewGroup contentview) {this.mtype = type_content;
 Showbytype (Mtype); /** * The corresponding display according to the type * @description: * @date 2016-2-19 pm 3:02:20/public void showbytype (int mtype) {hideal
  Lviews (); if (Mcontentview!= null) {switch (mtype) {case type_empty:if (Memptyorerrorview!= null) {Memptyor
      Errorview.setvisibility (view.visible);
      No_net.settext ("Here is empty also wild oh ~");
     Load_btn.setvisibility (View.gone);
    } break;
      Case Type_error:if (Memptyorerrorview!= null) {memptyorerrorview.setvisibility (view.visible);
      No_net.settext ("Network Load Failure Oh ~");
     Load_btn.setvisibility (view.visible);
    } break;
     Case Type_content:if (Mcontentview!= null) {mcontentview.setvisibility (view.visible);
    } break;
   Default:break; /** * Method Overview: Hide all Child view */private void HideallViews () {if (Mparentview!= null) {if (Mparentview.getchildcount () > 0) {for (int i = 0; i < Mparentview . Getchildcount ();
     i++) {View Childview = Mparentview.getchildat (i);
     if (Childview!= null) {childview.setvisibility (view.gone);
 }
    }
   }
  }

 }
}
 
Use the public class mainactivity the extends activity {private ListView ListView in the activity;
 Private Commonshowview Mshowview;

 Private list<string> datas;
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.activity_main);
  ListView = (ListView) Findviewbyid (R.id.listview);
  Mshowview = new Commonshowview (this, ListView);
  --------------//Datas = Getdatas (True) when----------have data;
  Listview.setadapter (This, datas) (new Myadapter); Mshowview.setcontextview (ListView),//display data, or without invocation, direct display//--------No data----------------//Datas = Getdatas (False
  )//No data//Mshowview.showbytype (Commonshowview.type_empty);
 ---------------Mshowview.showbytype (commonshowview.type_error)---------The failure of data loading;
  Private List<string> Getdatas (Boolean flag) {list<string> datas = new arraylist<> ();
  Datas.add ("This is the first line of data");
  Datas.add ("This is the 2nd line of data");
  Datas.add ("This is the third line of data"); DAtas.add ("This is the 4th line of data");
  if (flag) {return datas;
  else {return null;
  } class Myadapter extends Baseadapter {private list<string> datas;

  Private context Mcontext;
   Public Myadapter (Context Mcontext, list<string> datas) {this.mcontext = Mcontext;
  This.datas = datas;
  @Override public int GetCount () {//TODO auto-generated a stub return datas.size (); @Override public Object getitem (int position) {//TODO auto-generated Method stub return Datas.get (position)
  ;
  @Override public long getitemid (int position) {//TODO auto-generated method stub return position; @Override public View getview (int position, View Convertview, ViewGroup parent) {//Here is just a small example that does not consider performance tuning TEXTV
   Iew TV = new TextView (mcontext);
   Tv.settextsize (16f);
   Tv.settext (Datas.get (position));
  return TV;

 }

 }
}

At present, just made a super simple style, the specific display according to the need to change and encapsulation. Post a picture:

The above is the ListView data is empty and load error handling method, hope to be helpful to everybody's study.

Related Article

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.