最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解決Android ListView數(shù)據(jù)為空及加載錯誤的方法

 更新時間:2016年02月21日 16:17:08   作者:BetterLaterThanNever  
這篇文章主要為大家提供了一個解決Android ListView數(shù)據(jù)為空及加載錯誤的方法,感興趣的小伙伴們可以參考一下

在項目中,都會用到ListView或GridView等列表控件。一般會用來展示從網(wǎng)絡(luò)請求的數(shù)據(jù) 。如果請求的數(shù)據(jù)為空或者在請求的時候正好無沒有網(wǎng)絡(luò)了,我們的界面應(yīng)該如何展示呢?數(shù)據(jù)為空的時候,ListView可以使用setEmptyView (View emptyView) 方法來我們需要的統(tǒng)一界面。數(shù)據(jù)加載失敗呢?我們也可以統(tǒng)一進行處理。

//下面這個類是簡單地封裝用于無數(shù)據(jù)及加載錯誤的一個頁面。
public class CommonShowView {

 private Context mContext;// 上下文
 private ViewGroup mEmptyOrErrorView;// 頁面加載無數(shù)據(jù)或加載錯誤時顯示
 private ViewGroup mContentView;// 加載成功時顯示的內(nèi)容
 private ViewGroup mParentView;// 父布局viewGroup
 private LayoutInflater mInflater;
 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;// 數(shù)據(jù)為空
 public final static int TYPE_ERROR = 2;// 加載數(shù)據(jù)失敗
 public final static int TYPE_CONTENT = 3;// 直接顯示內(nèi)容
 private int mType = TYPE_EMPTY;// 數(shù)據(jù)類型,默認是無數(shù)據(jù)

 /**
  * 構(gòu)造方法,傳入上下文及內(nèi)容GroupView
  */
 public CommonShowView(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() {// 檢查網(wǎng)絡(luò),進行網(wǎng)絡(luò)設(shè)置

   @Override
   public void onClick(View v) {
    Intent intent = null;
    // 判斷手機系統(tǒng)的版本 即API大于10 就是3.0或以上版本及魅族手機
    if (android.os.Build.VERSION.SDK_INT > 10 && !android.os.Build.MANUFACTURER.equals("Meizu")) {
     intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
    }
    else if (android.os.Build.VERSION.SDK_INT > 17 && android.os.Build.MANUFACTURER.equals("Meizu")) {
     intent = 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;
 }

 /**
  * 設(shè)置無數(shù)據(jù)或加載錯誤view
  * @description:
  */
 public void setEmptyOrErrorView(ViewGroup emptyOrErrorView) {
  this.mParentView.removeView(this.mEmptyOrErrorView);
  this.mParentView.addView(emptyOrErrorView, mLayoutParams);
  this.mEmptyOrErrorView = emptyOrErrorView;
 }

 /**
  * 設(shè)置無數(shù)據(jù)或加載錯誤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;
 }

 /**
  * 獲取內(nèi)容view
  * @description:
  */
 public ViewGroup getContextView() {
  return mContentView;
 }

 /**
  * 方法概述:獲取內(nèi)容View的父布局
  */
 public ViewGroup getRootView() {
  return mParentView;
 }

 /**
  * 方法概述:設(shè)置展示內(nèi)容的視圖
  */
 public void setContextView(ViewGroup contentView) {
  this.mType = TYPE_CONTENT;
  showByType(mType);
 }

 /**
  * 根據(jù)類型進行對應(yīng)展示 
  * @description:
  * @date 2016-2-19 下午3:02:20
  */
 public void showByType(int mType) {
  hideAllViews();
  if (mContentView != null) {
   switch (mType) {
    case TYPE_EMPTY:

     if (mEmptyOrErrorView != null) {
      mEmptyOrErrorView.setVisibility(View.VISIBLE);
      no_net.setText("這里空空也野哦~");
      load_btn.setVisibility(View.GONE);
     }
     break;
    case TYPE_ERROR:
     if (mEmptyOrErrorView != null) {
      mEmptyOrErrorView.setVisibility(View.VISIBLE);
      no_net.setText("網(wǎng)絡(luò)加載失敗哦~");
      load_btn.setVisibility(View.VISIBLE);
     }
     break;
    case TYPE_CONTENT:
     if (mContentView != null) {
      mContentView.setVisibility(View.VISIBLE);
     }
     break;
    default:
     break;
   }
  }
 }

 /**
  * 方法概述:將所有的子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);
     }
    }
   }
  }

 }
}
//錯誤頁面及空頁面時布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/rel_null"
 android:layout_width="match_parent"
 android:layout_height="match_parent" >

 <TextView
  android:id="@+id/no_net"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_centerHorizontal="true"
  android:layout_centerInParent="true"
  android:gravity="center"
  android:text="好像沒網(wǎng)哦~"
  android:textColor="#333333"
  android:textSize="16sp" />

 <Button
  android:id="@+id/load_btn"
  android:layout_width="112dp"
  android:layout_height="40dp"
  android:layout_below="@+id/no_net"
  android:layout_centerHorizontal="true"
  android:layout_marginTop="15dp"
  android:background="#E12228"
  android:gravity="center"
  android:text="檢查網(wǎng)絡(luò)"
  android:textColor="#ffffff"
  android:textSize="14sp" />

</RelativeLayout>
//在Activity中使用
public class MainActivity extends Activity {
 private ListView listview;
 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);
  // ----------有數(shù)據(jù)的時情況下--------------
  // datas = getDatas(true);
  // listview.setAdapter(new MyAdapter(this, datas));
  // mShowView.setContextView(listview);//顯示數(shù)據(jù),也可以不調(diào)用,直接顯示
  // --------無數(shù)據(jù)的情況下----------------
  // datas = getDatas(false);//無數(shù)據(jù)
  // mShowView.showByType(CommonShowView.TYPE_EMPTY);
  // ---------數(shù)據(jù)加載失敗的情況下---------------
  mShowView.showByType(CommonShowView.TYPE_ERROR);
 }

 private List<String> getDatas(boolean flag) {
  List<String> datas = new ArrayList<>();
  datas.add("這是第一行的data");
  datas.add("這是第2行的data");
  datas.add("這是第三行的data");
  datas.add("這是第4行的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 method 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) {
   // 這里只舉個小例子,沒有考慮性能 優(yōu)化
   TextView tv = new TextView(mContext);
   tv.setTextSize(16f);
   tv.setText(datas.get(position));
   return tv;
  }

 }
}

目前只是做了個超級簡單的樣式,具體的展示根據(jù)需求再改再封裝。帖個圖:

以上就是ListView數(shù)據(jù)為空及加載錯誤處理的方法,希望對大家的學(xué)習(xí)有所幫助。

相關(guān)文章

  • android 自定義控件 自定義屬性詳細介紹

    android 自定義控件 自定義屬性詳細介紹

    在android相關(guān)應(yīng)用開發(fā)過程中,固定的一些屬性可能滿足不了開發(fā)的需求,所以在一些特殊情況下,需要自定義控件與屬性,本文將以此問題進行詳細介紹,需要的朋友可以參考下
    2012-11-11
  • Android實現(xiàn)拼圖小游戲

    Android實現(xiàn)拼圖小游戲

    這篇文章主要為大家詳細介紹了Android實現(xiàn)拼圖小游戲,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • Flutter質(zhì)感設(shè)計之底部導(dǎo)航

    Flutter質(zhì)感設(shè)計之底部導(dǎo)航

    這篇文章主要為大家詳細介紹了Flutter質(zhì)感設(shè)計之底部導(dǎo)航的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Android實現(xiàn)基于ZXing快速集成二維碼掃描功能

    Android實現(xiàn)基于ZXing快速集成二維碼掃描功能

    這篇文章主要為大家詳細介紹了Android二維碼掃描ZXing快速項目集成的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Android中的LeakCanary的原理詳解

    Android中的LeakCanary的原理詳解

    大家好,本篇文章主要講的是Android中的LeakCanary的原理詳解,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-02-02
  • Android里實現(xiàn)退出主程序的提示代碼

    Android里實現(xiàn)退出主程序的提示代碼

    當(dāng)用戶選擇"確定",就退出當(dāng)前的對話框。其中,有個很重要的函數(shù),Activity.finish(),通過調(diào)用這個函數(shù),退出當(dāng)前運行的整個Android程序
    2013-06-06
  • Android 文件夾顯示紅色嘆號的解決方法(必看)

    Android 文件夾顯示紅色嘆號的解決方法(必看)

    下面小編就為大家?guī)硪黄狝ndroid 文件夾顯示紅色嘆號的解決方法(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • Android編程實現(xiàn)保存圖片到系統(tǒng)圖庫的方法示例

    Android編程實現(xiàn)保存圖片到系統(tǒng)圖庫的方法示例

    這篇文章主要介紹了Android編程實現(xiàn)保存圖片到系統(tǒng)圖庫的方法,結(jié)合實例形式分析了Android保存圖片到系統(tǒng)圖庫的常見操作方法、注意事項與相關(guān)問題解決技巧,需要的朋友可以參考下
    2017-08-08
  • Android使用ListView實現(xiàn)滾輪的動畫效果實例

    Android使用ListView實現(xiàn)滾輪的動畫效果實例

    這篇文章主要介紹了Android使用ListView實現(xiàn)滾輪的動畫效果實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Android?NDK開發(fā)(C語言--聯(lián)合體與枚舉)

    Android?NDK開發(fā)(C語言--聯(lián)合體與枚舉)

    這篇文章主要介紹了Android?NDK開發(fā)C語言聯(lián)合體與枚舉,共用體是一種特殊的數(shù)據(jù)類型,允許您在相同的內(nèi)存位置存儲不同的數(shù)據(jù)類型。您可以定義一個帶有多成員的共用體,但是任何時候只能有一個成員帶有值。下面詳細介紹該內(nèi)容,需要的朋友可以參考一下
    2021-12-12

最新評論

松江区| 安图县| 尼勒克县| 樟树市| 汉川市| 台江县| 乳源| 崇阳县| 通山县| 玛沁县| 泉州市| 清流县| 清水河县| 崇仁县| 乐平市| 进贤县| 铜鼓县| 汝阳县| 阳信县| 漯河市| 合山市| 南陵县| 抚宁县| 鹰潭市| 凭祥市| 健康| 三都| 客服| 海安县| 米林县| 乐陵市| 三门县| 和硕县| 邵阳县| 台州市| 大理市| 土默特右旗| 禹城市| 开阳县| 湟中县| 景谷|