解決Android ListView數(shù)據(jù)為空及加載錯(cuò)誤的方法
在項(xiàng)目中,都會(huì)用到ListView或GridView等列表控件。一般會(huì)用來(lái)展示從網(wǎng)絡(luò)請(qǐng)求的數(shù)據(jù) 。如果請(qǐng)求的數(shù)據(jù)為空或者在請(qǐng)求的時(shí)候正好無(wú)沒(méi)有網(wǎng)絡(luò)了,我們的界面應(yīng)該如何展示呢?數(shù)據(jù)為空的時(shí)候,ListView可以使用setEmptyView (View emptyView) 方法來(lái)我們需要的統(tǒng)一界面。數(shù)據(jù)加載失敗呢?我們也可以統(tǒng)一進(jìn)行處理。
//下面這個(gè)類是簡(jiǎn)單地封裝用于無(wú)數(shù)據(jù)及加載錯(cuò)誤的一個(gè)頁(yè)面。 public class CommonShowView { private Context mContext;// 上下文 private ViewGroup mEmptyOrErrorView;// 頁(yè)面加載無(wú)數(shù)據(jù)或加載錯(cuò)誤時(shí)顯示 private ViewGroup mContentView;// 加載成功時(shí)顯示的內(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ù)類型,默認(rèn)是無(wú)數(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ò),進(jìn)行網(wǎng)絡(luò)設(shè)置 @Override public void onClick(View v) { Intent intent = null; // 判斷手機(jī)系統(tǒng)的版本 即API大于10 就是3.0或以上版本及魅族手機(jī) 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è)置無(wú)數(shù)據(jù)或加載錯(cuò)誤view * @description: */ public void setEmptyOrErrorView(ViewGroup emptyOrErrorView) { this.mParentView.removeView(this.mEmptyOrErrorView); this.mParentView.addView(emptyOrErrorView, mLayoutParams); this.mEmptyOrErrorView = emptyOrErrorView; } /** * 設(shè)置無(wú)數(shù)據(jù)或加載錯(cuò)誤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ù)類型進(jìn)行對(duì)應(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隱藏起來(lái) */ 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); } } } } } }
//錯(cuò)誤頁(yè)面及空頁(yè)面時(shí)布局 <?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="好像沒(méi)網(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ù)的時(shí)情況下-------------- // datas = getDatas(true); // listview.setAdapter(new MyAdapter(this, datas)); // mShowView.setContextView(listview);//顯示數(shù)據(jù),也可以不調(diào)用,直接顯示 // --------無(wú)數(shù)據(jù)的情況下---------------- // datas = getDatas(false);//無(wú)數(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) { // 這里只舉個(gè)小例子,沒(méi)有考慮性能 優(yōu)化 TextView tv = new TextView(mContext); tv.setTextSize(16f); tv.setText(datas.get(position)); return tv; } } }
目前只是做了個(gè)超級(jí)簡(jiǎn)單的樣式,具體的展示根據(jù)需求再改再封裝。帖個(gè)圖:
以上就是ListView數(shù)據(jù)為空及加載錯(cuò)誤處理的方法,希望對(duì)大家的學(xué)習(xí)有所幫助。
- java 查找list中重復(fù)數(shù)據(jù)實(shí)例詳解
- Android操作SQLite數(shù)據(jù)庫(kù)(增、刪、改、查、分頁(yè)等)及ListView顯示數(shù)據(jù)的方法詳解
- Android中ListView結(jié)合CheckBox實(shí)現(xiàn)數(shù)據(jù)批量選擇(全選、反選、全不選)
- Android編程使用ListView實(shí)現(xiàn)數(shù)據(jù)列表顯示的方法
- Android實(shí)現(xiàn)ListView分頁(yè)自動(dòng)加載數(shù)據(jù)的方法
- Android List刪除重復(fù)數(shù)據(jù)
相關(guān)文章
Android實(shí)現(xiàn)橫屏切換科學(xué)計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)橫屏切換科學(xué)計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06基于Flutter實(shí)現(xiàn)轉(zhuǎn)場(chǎng)動(dòng)效的示例代碼
動(dòng)畫(huà)經(jīng)常會(huì)用于場(chǎng)景切換,比如滑動(dòng),縮放,尺寸變化。Flutter?提供了Transition系列的動(dòng)畫(huà)組件,可以讓場(chǎng)景轉(zhuǎn)換動(dòng)畫(huà)變得更加簡(jiǎn)單。本文整理了常用的Transition組件的應(yīng)用,需要的可以參考一下2022-05-05Android基于Sensor感應(yīng)器獲取重力感應(yīng)加速度的方法
這篇文章主要介紹了Android基于Sensor感應(yīng)器獲取重力感應(yīng)加速度的方法,涉及Android使用Sensor類實(shí)現(xiàn)感應(yīng)重力變化的功能,需要的朋友可以參考下2015-12-12Flutter實(shí)現(xiàn)自定義篩選框的示例代碼
本文主要介紹了Flutter實(shí)現(xiàn)自定義篩選框的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07淺析Kotlin使用infix函數(shù)構(gòu)建可讀語(yǔ)法流程講解
這篇文章主要介紹了淺析Kotlin使用infix函數(shù)構(gòu)建可讀語(yǔ)法,我們?cè)贙otlin中就多次使用A to B這樣的語(yǔ)法結(jié)構(gòu)構(gòu)建鍵值對(duì),包括Kotlin自帶的mapOf()函數(shù),這種語(yǔ)法結(jié)構(gòu)的優(yōu)點(diǎn)是可讀性強(qiáng)2023-01-01Android中DrawerLayout+ViewPager滑動(dòng)沖突的解決方法
這篇文章主要為大家詳細(xì)介紹了Android中DrawerLayout+ViewPager滑動(dòng)沖突的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Android仿網(wǎng)易嚴(yán)選底部彈出菜單效果
這篇文章主要為大家詳細(xì)介紹了Android仿網(wǎng)易嚴(yán)選底部彈出菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07