日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android列表视图(List View)

發布時間:2025/4/16 Android 66 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android列表视图(List View) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Android列表視圖(ListView)

?

ListView是一個顯示滾動項列表的示視圖組(viewgroup),通過使用適配器(Adapter)把這些列表項自動插入到列表中。適配器比如從一個數組或是數據庫查詢獲取到數據,然后轉換每一項成為可放入到列表的視圖。

?

列表的顯示需要三個元素:

(1)????ListView:用來展示列表的view。

(2)????適配器:用來把數據映射到ListView上。

(3)????數據:具體的將被映射的字符串、圖片或是基本組件。

?

?圖1

對于我們如何使用適配器來動態插入view,參考鏈接:

http://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews

?

1.??????使用加載器(Using a Loader)

為了實現異步加載數據,且為了避免在查詢時阻塞APP主線程,從Android3.0開始引入CursorLoader,這是一種查詢Cursor的標準方式。當CursorLoader接收到Cursor結果(查詢結束),LoaderCallbacks接收到對onLoadFinished()方法的回調,此方法使用新的Cursor來更新適配器,然后列表視圖顯示這個結果。

?

雖然CursorLoader的API是從Android3.0(API 11)開始引入的,但可以通過使用支持的庫來使用:http://developer.android.com/tools/extras/support-library.html,但要求Android系統版本在1.6及之后的版本。

?

public class ListViewLoader extends ListActivity
? ? ? ? implements LoaderManager.LoaderCallbacks<Cursor> {

? ? // This is the Adapter being used to display the list's data
? ? SimpleCursorAdapter mAdapter;

? ? // These are the Contacts rows that we will retrieve
? ? static final String[] PROJECTION = new String[] {ContactsContract.Data._ID,
? ? ? ? ? ? ContactsContract.Data.DISPLAY_NAME};

? ? // This is the select criteria
? ? static final String SELECTION = "((" +
? ? ? ? ? ? ContactsContract.Data.DISPLAY_NAME + " NOTNULL)AND (" +
? ? ? ? ? ? ContactsContract.Data.DISPLAY_NAME + " != ''))";

? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);

? ? ? ? // Create a progress bar to display whilethe list loads
? ? ? ? ProgressBar progressBar = new ProgressBar(this);
? ? ? ? progressBar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
? ? ? ? ? ? ? ? LayoutParams.WRAP_CONTENT, Gravity.CENTER));
? ? ? ? progressBar.setIndeterminate(true);
? ? ? ? getListView().setEmptyView(progressBar);

? ? ? ? // Must add the progress bar to the root ofthe layout
? ? ? ? ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
? ? ? ? root.addView(progressBar);

? ? ? ? // For the cursor adapter, specify whichcolumns go into which views
? ? ? ? String[] fromColumns = {ContactsContract.Data.DISPLAY_NAME};
? ? ? ? int[] toViews = {android.R.id.text1}; // The TextViewin simple_list_item_1

? ? ? ? // Create an empty adapter we will use todisplay the loaded data.
? ? ? ? // We pass null for the cursor, then updateit in onLoadFinished()
? ? ? ? mAdapter = new SimpleCursorAdapter(this,
? ? ? ? ? ? ? ? android.R.layout.simple_list_item_1, null,
? ? ? ? ? ? ? ? fromColumns, toViews, 0);
? ? ? ? setListAdapter(mAdapter);

? ? ? ? // Prepare the loader. ?Eitherre-connect with an existing one,
? ? ? ? // or start a new one.
? ? ? ? getLoaderManager().initLoader(0, null, this);
? ? }

? ? // Called when a new Loader needs to be created
? ? public Loader<Cursor> onCreateLoader(int id, Bundle args) {
? ? ? ? // Now create and return a CursorLoaderthat will take care of
? ? ? ? // creating a Cursor for the data beingdisplayed.
? ? ? ? return new CursorLoader(this, ContactsContract.Data.CONTENT_URI,
? ? ? ? ? ? ? ? PROJECTION, SELECTION, null, null);
? ? }

? ? // Called when a previously created loader has finished loading
? ? public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
? ? ? ? // Swap the new cursor in. ?(Theframework will take care of closing the
? ? ? ? // old cursor once we return.)
? ? ? ? mAdapter.swapCursor(data);
? ? }

? ? // Called when a previously created loader is reset, making the dataunavailable
? ? public void onLoaderReset(Loader<Cursor> loader) {
? ? ? ? // This is called when the last Cursorprovided to onLoadFinished()
? ? ? ? // above is about to be closed. ?We needto make sure we are no
? ? ? ? // longer using it.
? ? ? ? mAdapter.swapCursor(null);
? ? }

? ? @Override
? ? public void onListItemClick(ListView l, View v, int position, long id) {
? ? ? ? // Do something when a list item is clicked
? ? }
}

?

鏈接:

Android開發者:

http://developer.android.com/guide/topics/ui/layout/listview.html

?

AndroidListView原理學習與優化總結

http://mzh3344258.blog.51cto.com/1823534/889879

?

androidListView詳解

http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html

Android的CursorLoader用法小結

http://www.linuxidc.com/Linux/2013-05/84572.htm

?

總結

以上是生活随笔為你收集整理的Android列表视图(List View)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。