The Android-----arrayadapter rewrite.

Source: Internet
Author: User

Citation: http://blog.csdn.net/jason0539/article/details/9918465

Recently need to use Arrayadapter, save, later convenient to find

Arrayadapter (array adapter) is typically used to display a single line of text information, so it is easier.

 Public Arrayadapter (context context,int Textviewresourceid, list<t> objects)

The above line of code to assemble the data, to assemble the data requires a connection to the ListView object and array of data adapter to do both of the matching work, the construction of Arrayadapter requires three parameters, which is this, Layout file (Note that the layout file here describes the layout of each row of the list), the data source (a list collection). Bind the ListView and adapter together with Setadapter ().

Example one: It can be used to display a text in a simple way

Package Jason.demo14;import Java.util.arraylist;import android.app.activity;import android.os.bundle;import Android.widget.arrayadapter;import Android.widget.ListView; Public classDemo13activity extends Activity {PrivateListView LV; Privatearraylist<string> list =NewArraylist<string>(); @Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main); LV=(ListView) Findviewbyid (R.id.listview); Arrayadapter<String> adapter =NewArrayadapter<string>(                 This, Android.        R.layout.simple_expandable_list_item_1, GetData ());    Lv.setadapter (adapter); }        PrivateArraylist<string>GetData () {List.add ("180 square meters of house"); List.add ("a hardworking, beautiful wife."); List.add ("a BMW"); List.add ("a strong and never sick body."); List.add ("a favorite career."); returnlist; }}<span style="Background-color: #ffffff; Font-family:arial"></SPAN>

Note: Here's Android. R.layout.simple_expandable_list_item_1 is the system built-in layout, the style is as follows

Example two: This example can add a imageview, but you need to add this custom layout when setting up Arrayadapter </SPAN>

 Public int int Textviewresourceid, list<t> objects)

The first parameter above is the context, which is typically this. The second parameter is a custom layout file, such as R.layout.list_item. The third parameter is the ID of the TextView used to display the text in the layout, and the fourth parameter is the data collection, as in example one

<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Android:layout_width="match_parent"Android:layout_height="match_parent"android:orientation="Horizontal"> <ImageView Android:id="@+id/img"Android:layout_width="wrap_content"Android:layout_height="wrap_content"android:src="@drawable/ic_launcher"/> <TextView Android:id="@+id/tv"Android:layout_width="fill_parent"Android:layout_height="wrap_content"/></linearlayout>

Java code for activity:

Package Jason.demo14;import Java.util.arraylist;import android.app.activity;import android.os.bundle;import Android.widget.arrayadapter;import Android.widget.ListView; Public classDemo13activity extends Activity {PrivateListView LV; Privatearraylist<string> list =NewArraylist<string>(); @Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main); LV=(ListView) Findviewbyid (R.id.listview); Arrayadapter<String> adapter =NewArrayadapter<string>(                 This, R.layout.list_item,//The difference is in this line and next line, here is your own definition of the layout file,R.id.tv,//This is the ID of TextView in R.layout.list_item, which is used to determine where the added data is displayedgetData ());    Lv.setadapter (adapter); }        PrivateArraylist<string>GetData () {List.add ("180 square meters of house"); List.add ("a hardworking, beautiful wife."); List.add ("a BMW"); List.add ("a strong and never sick body."); List.add ("a favorite career."); returnlist; }}

The effect is as follows:

Example 3: Achieve more complex effects:

This requires rewriting the GetView method,

1. Custom list Styles

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

 Public classImagelistadapter extends Arrayadapter<file>{     Private intresource;  PublicImagelistadapter (Context context,intResourceId, list<file>objects)         {Super (context, resourceId, objects); //log it down later usingResource =resourceId; }     PublicView GetView (intposition, View Convertview, ViewGroup parent)         {LinearLayout Imagelistview; //Get DataFile File =GetItem (position); String FileName=File.getname (); Bitmap Bitmap=getbitmapfromfile (file); //when the system displays the list, first instantiate an adapter (this will instantiate the custom adapter). //When you manually complete the adaptation, you must manually map the data, which requires overriding the GetView () method. //This method is called when the system draws each row of the list. //GetView () has three parameters,//position that the first line will be displayed,//Covertview is the layout inflate from the layout file. //we use the Layoutinflater method to extract the well-defined Image_item.xml file into a view instance for display. //The individual components in the XML file are then instantiated (the Simple Findviewbyid () method). //This allows the data to be mapped to individual components. //         if(Convertview = =NULL) {Imagelistview=NewLinearLayout (GetContext ()); Layoutinflater Inflater=(Layoutinflater) GetContext (). Getsystemservice (Context.layout_inflater_service); Inflater.inflate (Resource, Imagelistview,true);//to parse the Image_item.xml layout into the LinearLayout .}Else{Imagelistview=(linearlayout) Convertview; }        //get control, populate dataImageView ImageView =(ImageView) Imagelistview.findviewbyid (R.id.item_thumbnail); TextView TextView=(TextView) Imagelistview.findviewbyid (r.id.item_file_name);         Textview.settext (FileName);        Imageview.setimagebitmap (bitmap); returnImagelistview; }    //get bitmap from file for populating    PrivateBitmap getbitmapfromfile (file file) {Bitmap Bitmap=Bitmapfactory.decodefile (File.getabsolutepath ()); returnbitmap; } }

3. Binding Data

Private void bindfilestolist (file[] files) {         Listnew arraylist<file>();           for (file file:files) {             filelist.add (file);         }         New Imagelistadapter (imagefileslistactivity.  this,                                                         R.layout.image_item,                                                         fileList);         Setlistadapter (adapter); }

That's basically it.

The Android-----arrayadapter rewrite.

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.