این صفحه به شما نشان می دهد که چگونه QuickContactBadge را به UI خود اضافه کنید و چگونه داده ها را به آن متصل کنید. QuickContactBadge ویجتی است که در ابتدا به عنوان یک تصویر کوچک ظاهر می شود. اگرچه میتوانید از هر Bitmap برای تصویر کوچک استفاده کنید، اما معمولاً از یک Bitmap رمزگشایی شده از تصویر کوچک عکس مخاطب استفاده میکنید.
تصویر کوچک به عنوان یک کنترل عمل می کند. هنگامی که کاربران روی تصویر ضربه می زنند، QuickContactBadge به یک گفتگو شامل موارد زیر گسترش می یابد:
- یک تصویر بزرگ
- تصویر بزرگ مرتبط با مخاطب یا، اگر تصویری در دسترس نباشد، یک گرافیک نگهدارنده مکان.
- نمادهای برنامه
- یک نماد برنامه برای هر بخش از داده های جزئیات که می تواند توسط یک برنامه داخلی مدیریت شود. به عنوان مثال، اگر جزئیات مخاطب شامل یک یا چند آدرس ایمیل باشد، یک نماد ایمیل ظاهر می شود. وقتی کاربران روی نماد ضربه می زنند، تمام آدرس های ایمیل مخاطب ظاهر می شود. هنگامی که کاربران روی یکی از آدرسها ضربه میزنند، برنامه ایمیل صفحهای برای نوشتن پیام به آدرس ایمیل انتخابی نمایش میدهد.
نمای QuickContactBadge دسترسی فوری به جزئیات مخاطب و راهی سریع برای برقراری ارتباط با مخاطب را فراهم می کند. کاربران مجبور نیستند مخاطبی را جستجو کنند، اطلاعات را بیابند و کپی کنند و سپس آن را در برنامه مناسب جایگذاری کنند. در عوض، آنها می توانند روی QuickContactBadge ضربه بزنند، روش ارتباطی مورد نظر خود را انتخاب کنند و اطلاعات مربوط به آن روش را مستقیماً به برنامه مربوطه ارسال کنند.
یک نمای QuickContactBadge اضافه کنید
برای افزودن QuickContactBadge ، یک عنصر <QuickContactBadge> را در طرح خود وارد کنید، همانطور که در مثال زیر نشان داده شده است:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> ... <QuickContactBadge android:id=@+id/quickbadge android:layout_height="wrap_content" android:layout_width="wrap_content" android:scaleType="centerCrop"/> ... </RelativeLayout>
بازیابی داده های ارائه دهنده
برای نمایش یک مخاطب در QuickContactBadge ، به یک URI محتوا برای مخاطب و یک Bitmap برای تصویر کوچک نیاز دارید. شما هم URI محتوا و هم Bitmap را از ستون های بازیابی شده از Contacts Provider تولید می کنید. این ستون ها را به عنوان بخشی از طرح ریزی که برای بارگذاری داده ها در Cursor استفاده می کنید، مشخص کنید.
برای Android نسخه 3.0 (سطح API 11) و بالاتر، ستونهای زیر را در طرح خود قرار دهید:
برای Android 2.3.3 (سطح API 10) و پایین تر، از ستون های زیر استفاده کنید:
مثالهای موجود در این صفحه فرض میکنند که Cursor حاوی این ستونها و هر ستون انتخابی دیگری بارگذاری شده است. برای یادگیری نحوه بازیابی ستون ها در Cursor ، به بازیابی لیست مخاطبین مراجعه کنید.
URI مخاطب و تصویر کوچک را تنظیم کنید
هنگامی که ستون های لازم را دارید، می توانید داده ها را به QuickContactBadge متصل کنید.
URI مخاطب را تنظیم کنید
برای تنظیم URI محتوا برای مخاطب، با getLookupUri(id,lookupKey) تماس بگیرید تا یک CONTENT_LOOKUP_URI دریافت کنید، سپس با assignContactUri() تماس بگیرید تا مخاطب را تنظیم کنید. این در مثال زیر نشان داده شده است:
کاتلین
// The Cursor that contains contact rows var cursor: Cursor? = null // The index of the _ID column in the Cursor var idColumn: Int = 0 // The index of the LOOKUP_KEY column in the Cursor var lookupKeyColumn: Int = 0 // A content URI for the desired contact var contactUri: Uri? = null // A handle to the QuickContactBadge view ... cursor?.let { cursor -> /* * Insert code here to move to the desired cursor row */ // Gets the _ID column index idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID) // Gets the LOOKUP_KEY index lookupKeyColumn = cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY) // Gets a content URI for the contact contactUri = ContactsContract.Contacts.getLookupUri( cursor.getLong(idColumn), cursor.getString(lookupKeyColumn) ) binding.badge.assignContactUri(contactUri) }
جاوا
// The Cursor that contains contact rows Cursor cursor; // The index of the _ID column in the Cursor int idColumn; // The index of the LOOKUP_KEY column in the Cursor int lookupKeyColumn; // A content URI for the desired contact Uri contactUri; ... /* * Insert code here to move to the desired cursor row */ // Gets the _ID column index idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID); // Gets the LOOKUP_KEY index lookupKeyColumn = cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY); // Gets a content URI for the contact contactUri = Contacts.getLookupUri( cursor.getLong(idColumn), cursor.getString(lookupKeyColumn) ); binding.badge.assignContactUri(contactUri);
وقتی کاربران روی نماد QuickContactBadge ضربه می زنند، جزئیات مخاطب در گفتگو ظاهر می شود.
تصویر کوچک عکس را تنظیم کنید
تنظیم URI مخاطب برای QuickContactBadge به طور خودکار عکس کوچک مخاطب را بارگیری نمی کند. برای بارگذاری عکس، یک URI برای عکس از ردیف Cursor مخاطب دریافت کنید، از آن برای باز کردن فایل حاوی عکس کوچک فشرده شده استفاده کنید و فایل را در Bitmap بخوانید.
توجه: ستون PHOTO_THUMBNAIL_URI در نسخههای پلتفرم قبل از نسخه 3.0 موجود نیست. برای آن نسخه ها، باید URI را از جدول فرعی Contacts.Photo بازیابی کنید.
ابتدا، متغیرهایی را برای دسترسی به Cursor حاوی ستون های Contacts._ID و Contacts.LOOKUP_KEY تنظیم کنید:
کاتلین
// The column in which to find the thumbnail ID var thumbnailColumn: Int = 0 /* * The thumbnail URI, expressed as a String. * Contacts Provider stores URIs as String values. */ var thumbnailUri: String? = null ... cursor?.let { cursor -> /* * Gets the photo thumbnail column index if * platform version >= Honeycomb */ thumbnailColumn = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI) // Otherwise, sets the thumbnail column to the _ID column } else { idColumn } /* * Assuming the current Cursor position is the contact you want, * gets the thumbnail ID */ thumbnailUri = cursor.getString(thumbnailColumn) }
جاوا
// The column in which to find the thumbnail ID int thumbnailColumn; /* * The thumbnail URI, expressed as a String. * Contacts Provider stores URIs as String values. */ String thumbnailUri; ... /* * Gets the photo thumbnail column index if * platform version >= Honeycomb */ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { thumbnailColumn = cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI); // Otherwise, sets the thumbnail column to the _ID column } else { thumbnailColumn = idColumn; } /* * Assuming the current Cursor position is the contact you want, * gets the thumbnail ID */ thumbnailUri = cursor.getString(thumbnailColumn); ...
روشی را تعریف کنید که داده های مربوط به عکس را برای مخاطب و ابعاد نمای مقصد می گیرد و تصویر کوچک با اندازه مناسب را در Bitmap برمی گرداند. با ساختن یک URI که به تصویر کوچک اشاره می کند شروع کنید:
کاتلین
/** * Load a contact photo thumbnail and return it as a Bitmap, * resizing the image to the provided image dimensions as needed. * @param photoData photo ID Prior to Honeycomb, the contact's _ID value. * For Honeycomb and later, the value of PHOTO_THUMBNAIL_URI. * @return A thumbnail Bitmap, sized to the provided width and height. * Returns null if the thumbnail is not found. */ private fun loadContactPhotoThumbnail(photoData: String): Bitmap? { // Creates an asset file descriptor for the thumbnail file var afd: AssetFileDescriptor? = null // try-catch block for file not found try { // Creates a holder for the URI val thumbUri: Uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { // If Android 3.0 or later, // sets the URI from the incoming PHOTO_THUMBNAIL_URI Uri.parse(photoData) } else { // Prior to Android 3.0, constructs a photo Uri using _ID /* * Creates a contact URI from the Contacts content URI * incoming photoData (_ID) */ val contactUri: Uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, photoData) /* * Creates a photo URI by appending the content URI of * Contacts.Photo */ Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY) } /* * Retrieves an AssetFileDescriptor object for the thumbnail URI * using ContentResolver.openAssetFileDescriptor */ afd = activity?.contentResolver?.openAssetFileDescriptor(thumbUri, "r") /* * Gets a file descriptor from the asset file descriptor. * This object can be used across processes. */ return afd?.fileDescriptor?.let {fileDescriptor -> // Decodes the photo file and returns the result as a Bitmap // if the file descriptor is valid BitmapFactory.decodeFileDescriptor(fileDescriptor, null, null) } } catch (e: FileNotFoundException) { /* * Handle file not found errors */ null } finally { // In all cases, close the asset file descriptor try { afd?.close() } catch (e: IOException) { } } }
جاوا
/** * Load a contact photo thumbnail and return it as a Bitmap, * resizing the image to the provided image dimensions as needed. * @param photoData photo ID Prior to Honeycomb, the contact's _ID value. * For Honeycomb and later, the value of PHOTO_THUMBNAIL_URI. * @return A thumbnail Bitmap, sized to the provided width and height. * Returns null if the thumbnail is not found. */ private Bitmap loadContactPhotoThumbnail(String photoData) { // Creates an asset file descriptor for the thumbnail file AssetFileDescriptor afd = null; // try-catch block for file not found try { // Creates a holder for the URI Uri thumbUri; // If Android 3.0 or later if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { // Sets the URI from the incoming PHOTO_THUMBNAIL_URI thumbUri = Uri.parse(photoData); } else { // Prior to Android 3.0, constructs a photo Uri using _ID /* * Creates a contact URI from the Contacts content URI * incoming photoData (_ID) */ final Uri contactUri = Uri.withAppendedPath( ContactsContract.Contacts.CONTENT_URI, photoData); /* * Creates a photo URI by appending the content URI of * Contacts.Photo */ thumbUri = Uri.withAppendedPath( contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); } /* * Retrieves an AssetFileDescriptor object for the thumbnail URI * using ContentResolver.openAssetFileDescriptor */ afd = getActivity().getContentResolver(). openAssetFileDescriptor(thumbUri, "r"); /* * Gets a file descriptor from the asset file descriptor. * This object can be used across processes. */ FileDescriptor fileDescriptor = afd.getFileDescriptor(); // Decodes the photo file and returns the result as a Bitmap // if the file descriptor is valid if (fileDescriptor != null) { // Decodes the bitmap return BitmapFactory.decodeFileDescriptor( fileDescriptor, null, null); } // If the file isn't found } catch (FileNotFoundException e) { /* * Handle file not found errors */ // In all cases, close the asset file descriptor } finally { if (afd != null) { try { afd.close(); } catch (IOException e) {} } } return null; }
متد loadContactPhotoThumbnail() را در کد خود فراخوانی کنید تا تصویر کوچک Bitmap دریافت کنید و از نتیجه برای تنظیم تصویر کوچک عکس در QuickContactBadge خود استفاده کنید:
کاتلین
... /* * Decodes the thumbnail file to a Bitmap */ mThumbnailUri?.also { thumbnailUri -> loadContactPhotoThumbnail(thumbnailUri).also { thumbnail -> /* * Sets the image in the QuickContactBadge. * QuickContactBadge inherits from ImageView. */ badge.setImageBitmap(thumbnail) } }
جاوا
... /* * Decodes the thumbnail file to a Bitmap */ Bitmap mThumbnail = loadContactPhotoThumbnail(thumbnailUri); /* * Sets the image in the QuickContactBadge. * QuickContactBadge inherits from ImageView. */ badge.setImageBitmap(mThumbnail);
یک QuickContactBadge به ListView اضافه کنید
QuickContactBadge یک افزونه مفید برای ListView است که لیستی از مخاطبین را نمایش می دهد. از QuickContactBadge برای نمایش یک عکس کوچک برای هر مخاطب استفاده کنید. هنگامی که کاربران روی تصویر کوچک ضربه می زنند، گفتگوی QuickContactBadge ظاهر می شود.
عنصر QuickContactBadge را اضافه کنید
برای شروع، یک عنصر نمای QuickContactBadge را به طرح بندی مورد خود اضافه کنید، به عنوان مثال، اگر می خواهید یک QuickContactBadge و یک نام برای هر مخاطبی که بازیابی می کنید نمایش دهید، XML زیر را در یک فایل طرح بندی قرار دهید:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <QuickContactBadge android:id="@+id/quickcontact" android:layout_height="wrap_content" android:layout_width="wrap_content" android:scaleType="centerCrop"/> <TextView android:id="@+id/displayname" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@+id/quickcontact" android:gravity="center_vertical" android:layout_alignParentRight="true" android:layout_alignParentTop="true"/> </RelativeLayout>
در بخشهای بعدی، به این فایل contact_item_layout.xml اشاره میشود.
یک CursorAdapter سفارشی تنظیم کنید
برای اتصال CursorAdapter به ListView حاوی QuickContactBadge ، یک آداپتور سفارشی تعریف کنید که CursorAdapter گسترش دهد. این رویکرد به شما امکان میدهد دادههای موجود در Cursor را قبل از اتصال به QuickContactBadge پردازش کنید. این رویکرد همچنین به شما امکان می دهد چندین ستون Cursor به QuickContactBadge متصل کنید. هیچ یک از این عملیات در یک CursorAdapter معمولی امکان پذیر نیست.
زیر کلاس CursorAdapter که تعریف میکنید باید روشهای زیر را لغو کند:
-
CursorAdapter.newView() - برای نگه داشتن طرح بندی مورد، یک شی
Viewجدید را باد می کند. در نادیده گرفتن این روش، دستگیرهها را در فرزندViewاشیاء طرحبندی، از جمله فرزندQuickContactBadgeذخیره کنید. با در نظر گرفتن این رویکرد، هر بار که یک طرحبندی جدید را ایجاد میکنید، از گرفتن دستهها برایViewاشیاء کودک اجتناب میکنید.شما باید این روش را نادیده بگیرید تا بتوانید دستههایی را به تک تک اشیاء
Viewفرزند دریافت کنید. این تکنیک به شما امکان می دهد اتصال آنها را درCursorAdapter.bindView()کنترل کنید. -
CursorAdapter.bindView() - داده ها را از ردیف
Cursorفعلی به فرزندViewاشیاء طرح بندی آیتم منتقل می کند. شما باید این روش را لغو کنید تا بتوانید URI و تصویر کوچک مخاطب را بهQuickContactBadgeمتصل کنید. پیادهسازی پیشفرض فقط اجازه نگاشت یک به یک بین یک ستون و یکViewرا میدهد.
قطعه کد زیر شامل نمونه ای از زیر کلاس سفارشی CursorAdapter است:
آداپتور لیست سفارشی را تعریف کنید
زیر کلاس CursorAdapter را از جمله سازنده آن تعریف کنید و newView() و bindView() نادیده بگیرید:
کاتلین
/** * Defines a class that holds resource IDs of each item layout * row to prevent having to look them up each time data is * bound to a row */ private data class ViewHolder( internal var displayname: TextView? = null, internal var quickcontact: QuickContactBadge? = null ) /** * * */ private inner class ContactsAdapter( context: Context, val inflater: LayoutInflater = LayoutInflater.from(context) ) : CursorAdapter(context, null, 0) { ... override fun newView( context: Context, cursor: Cursor, viewGroup: ViewGroup ): View { /* Inflates the item layout. Stores view references * in a ViewHolder class to prevent having to look * them up each time bindView() is called. */ return ContactListLayoutBinding.inflate(inflater, viewGroup, false).also { binding -> view.tag = ViewHolder().apply { displayname = binding.displayname quickcontact = binding.quickcontact } }.root } ... override fun bindView(view: View?, context: Context?, cursor: Cursor?) { (view?.tag as? ViewHolder)?.also { holder -> cursor?.apply { ... // Sets the display name in the layout holder.displayname?.text = getString(displayNameIndex) ... /* * Generates a contact URI for the QuickContactBadge */ ContactsContract.Contacts.getLookupUri( getLong(idIndex), cursor.getString(lookupKeyIndex) ).also { contactUri -> holder.quickcontact?.assignContactUri(contactUri) } getString(photoDataIndex)?.also {photoData -> /* * Decodes the thumbnail file to a Bitmap. * The method loadContactPhotoThumbnail() is defined * in the section "Set the contact URI and thumbnail." */ loadContactPhotoThumbnail(photoData)?.also { thumbnailBitmap -> /* * Sets the image in the QuickContactBadge. * QuickContactBadge inherits from ImageView. */ holder.quickcontact?.setImageBitmap(thumbnailBitmap) } } } } } }
جاوا
private class ContactsAdapter extends CursorAdapter { private LayoutInflater inflater; ... public ContactsAdapter(Context context) { super(context, null, 0); /* * Gets an inflater that can instantiate * the ListView layout from the file */ inflater = LayoutInflater.from(context); ... } ... /** * Defines a class that holds resource IDs of each item layout * row to prevent having to look them up each time data is * bound to a row */ private class ViewHolder { TextView displayname; QuickContactBadge quickcontact; } ... @Override public View newView( Context context, Cursor cursor, ViewGroup viewGroup) { /* Inflates the item layout. Stores view references * in a ViewHolder class to prevent having to look * them up each time bindView() is called. */ final ContactListLayoutBinding binding = ContactListLayoutBinding.inflate(inflater, viewGroup, false); final ViewHolder holder = new ViewHolder(); holder.displayname = binding.displayName; holder.quickcontact = binding.quickContact; view.setTag(holder); return binding.root; } ... @Override public void bindView( View view, Context context, Cursor cursor) { final ViewHolder holder = (ViewHolder) view.getTag(); final String photoData = cursor.getString(photoDataIndex); final String displayName = cursor.getString(displayNameIndex); ... // Sets the display name in the layout holder.displayname = cursor.getString(displayNameIndex); ... /* * Generates a contact URI for the QuickContactBadge */ final Uri contactUri = Contacts.getLookupUri( cursor.getLong(idIndex), cursor.getString(lookupKeyIndex)); holder.quickcontact.assignContactUri(contactUri); String photoData = cursor.getString(photoDataIndex); /* * Decodes the thumbnail file to a Bitmap. * The method loadContactPhotoThumbnail() is defined * in the section "Set the contact URI and thumbnail." */ Bitmap thumbnailBitmap = loadContactPhotoThumbnail(photoData); /* * Sets the image in the QuickContactBadge. * QuickContactBadge inherits from ImageView. */ holder.quickcontact.setImageBitmap(thumbnailBitmap); }
متغیرها را تنظیم کنید
همانطور که در مثال زیر نشان داده شده است، در کد خود، متغیرهایی از جمله نمایش Cursor که شامل ستون های لازم است را تنظیم کنید.
نکته: قطعه کد زیر از متد loadContactPhotoThumbnail() استفاده می کند که در بخش Set the contact URI and thumbnail تعریف شده است.
کاتلین
/* * Defines a projection based on platform version. This ensures * that you retrieve the correct columns. */ private val PROJECTION: Array<out String> = arrayOf( ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY, if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { ContactsContract.Contacts.DISPLAY_NAME_PRIMARY } else { ContactsContract.Contacts.DISPLAY_NAME }, if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { ContactsContract.Contacts.PHOTO_FILE_ID } else { /* * Although it's not necessary to include the * column twice, this keeps the number of * columns the same regardless of version */ ContactsContract.Contacts._ID } ) ... class ContactsFragment : Fragment(), LoaderManager.LoaderCallbacks<Cursor> { ... // Defines a ListView private val listView: ListView? = null // Defines a ContactsAdapter private val adapter: ContactsAdapter? = null ... // Defines a Cursor to contain the retrieved data private val cursor: Cursor? = null /* * As a shortcut, defines constants for the * column indexes in the Cursor. The index is * 0-based and always matches the column order * in the projection. */ // Column index of the _ID column private val idIndex = 0 // Column index of the LOOKUP_KEY column private val lookupKeyIndex = 1 // Column index of the display name column private val displayNameIndex = 3 /* * Column index of the photo data column. * It's PHOTO_THUMBNAIL_URI for Honeycomb and later, * and _ID for previous versions. */ private val photoDataIndex: Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) 3 else 0 ...
جاوا
public class ContactsFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> { ... // Defines a ListView private ListView listView; // Defines a ContactsAdapter private ContactsAdapter adapter; ... // Defines a Cursor to contain the retrieved data private Cursor cursor; /* * Defines a projection based on platform version. This ensures * that you retrieve the correct columns. */ private static final String[] PROJECTION = { ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY, (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) ? ContactsContract.Contacts.DISPLAY_NAME_PRIMARY : ContactsContract.Contacts.DISPLAY_NAME (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) ? ContactsContract.Contacts.PHOTO_FILE_ID : /* * Although it's not necessary to include the * column twice, this keeps the number of * columns the same regardless of version */ ContactsContract.Contacts._ID }; /* * As a shortcut, defines constants for the * column indexes in the Cursor. The index is * 0-based and always matches the column order * in the projection. */ // Column index of the _ID column private int idIndex = 0; // Column index of the LOOKUP_KEY column private int lookupKeyIndex = 1; // Column index of the display name column private int displayNameIndex = 3; /* * Column index of the photo data column. * It's PHOTO_THUMBNAIL_URI for Honeycomb and later, * and _ID for previous versions. */ private int photoDataIndex = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? 3 : 0; ...
ListView را تنظیم کنید
در Fragment.onCreate() آداپتور مکان نما سفارشی را نمونه سازی کنید و یک دسته برای ListView دریافت کنید:
کاتلین
override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return FragmentListViewBinding.inflate(...).let { binding -> ... /* * Gets a handle to the ListView in the file * contact_list_layout.xml */ listView = binding.contactList mAdapter?.also { listView?.adapter = it } ... }.root } ...
جاوا
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { FragmentListViewBinding binding = FragmentListViewBinding.inflate(...) ... /* * Gets a handle to the ListView in the file * contact_list_layout.xml */ if (binding.contactListView != null && adapter != null) { binding.contactListView.setAdapter(adapter); } ... } ...
در onViewCreated() ContactsAdapter را به ListView متصل کنید:
کاتلین
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) /* * Instantiates the subclass of * CursorAdapter */ mAdapter = activity?.let { ContactsAdapter(it).also { adapter -> // Sets up the adapter for the ListView listView?.adapter = adapter } } }
جاوا
@Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { ... /* * Instantiates the subclass of * CursorAdapter */ mAdapter = new ContactsAdapter(getActivity()); // Sets up the adapter for the ListView if (listView != null && mAdapter != null) { listView.setAdapter(mAdapter); } ... } ...
هنگامی که Cursor حاوی اطلاعات مخاطبین را دریافت می کنید، معمولاً در onLoadFinished() ، با swapCursor() تماس بگیرید تا داده های Cursor را به ListView منتقل کنید. این QuickContactBadge برای هر ورودی در لیست مخاطبین نمایش داده می شود.
کاتلین
override fun onLoadFinished(loader: Loader<Cursor>, cursor: Cursor) { // When the loader has completed, swap the cursor into the adapter mAdapter?.swapCursor(cursor) }
جاوا
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { // When the loader has completed, swap the cursor into the adapter mAdapter.swapCursor(cursor); }
هنگامی که یک Cursor با یک CursorAdapter (یا زیر کلاس) به ListView متصل می کنید، و از CursorLoader برای بارگیری Cursor استفاده می کنید، همیشه در اجرای onLoaderReset() ارجاعات به Cursor را پاک کنید. این در مثال زیر نشان داده شده است:
کاتلین
override fun onLoaderReset(loader: Loader<Cursor>) { // Removes remaining reference to the previous Cursor adapter?.swapCursor(null) }
جاوا
@Override public void onLoaderReset(Loader<Cursor> loader) { // Removes remaining reference to the previous Cursor adapter.swapCursor(null); }