[Android Development] How to customize arrayadapter

Source: Internet
Author: User

Scenario:

I want to display all the images in the/dcim/camera directory of the SD card to a list. On the left side of each item in this list is the thumbnail of the image, and on the right side is the file name.

Analysis:

This application is very typical.Data BindingAddCustom display.

How can this problem be solved?

1. Customize the list Style

Image_item.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:orientation="horizontal"> <ImageView     android:id="@+id/item_thumbnail"     android:layout_height="48dip"     android:layout_width="48dip"         /> <TextView     android:id="@+id/item_file_name"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:textAppearance="?android:attr/textAppearanceLarge"     android:minHeight="?android:attr/listPreferredItemHeight"     android:gravity="center_vertical"     android:paddingLeft="5dip"     /></LinearLayout>

2. Custom arrayadapter

Imagelistadapter

Public class imagelistadapter extends arrayadapter <File> {private int resource; Public imagelistadapter (context, int resourceid, list <File> objects) {super (context, resourceid, objects ); // record it and use resource = resourceid later;} public view getview (INT position, view convertview, viewgroup parent) {linearlayout imagelistview; // obtain the data file = getitem (position ); string filename = file. getnam E (); Bitmap bitmap = getbitmapfromfile (File); // when the system displays the list, an adapter is first instantiated (the custom adapter will be instantiated here ). // When manual adaptation is completed, data must be manually mapped. You need to override the getview () method. // The system calls this method when drawing each row of the list. // Getview () has three parameters. // position indicates the rows to be displayed. // covertview indicates the inflate layout from the layout file. // We use the layoutinflater method to extract the defined image_item.xml file into a view instance for display. // Instantiate each component in the XML file (simple findviewbyid () method ). // You can map the data to each component. // If (convertview = NULL) {imagelistview = new linearlayout (getcontext ()); // check the definition of layoutinflater In the android document. // This class is used to instantiate layout XML file into its corresponding view objects. // It is never be used directly -- use getlayoutinflater () or getsystemservice (string) // to retrieve a standard layoutinflater instance that is already hooked up to the current // context and correctly configured for the device you are running on .. for example: String Inflater = context. layout_inflater_service; layoutinflater Vi = (layoutinflater) getcontext (). getsystemservice (Inflater); VI. inflate (resource, imagelistview, true); // This log is called every time you select images. D ("adapter", "convertview is null now");} else {// it is strange that imagelistview = (linearlayout) convertview; log has not been called. D ("adapter", "convertview is not null now");} // fill in custom data imageview = (imageview) imagelistview. findviewbyid (R. id. item_thumbnail); textview = (textview) imagelistview. findviewbyid (R. id. item_file_name); textview. settext (filename); imageview. setimagebitmap (Bitmap); Return imagelistview;} // obtain the bitmap from the file to fill in the private bitmap getbitmapfromfile (File file) {Bitmap bitmap = bitmapfactory. decodefile (file. getabsolutepath (); Return bitmap ;}}

3. Bind data

private void bindFilesToList(File[] files) {         List<File> fileList = new ArrayList<File>();         for(File file : files) {             fileList.add(file);         }        ImageListAdapter adapter = new ImageListAdapter(ImageFilesListActivity.this,                                                         R.layout.image_item,                                                         fileList);         setListAdapter(adapter); }

This is basically the case.

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.