Customizing the contents of the item inside the listview

Source: Internet
Author: User

We can not meet just to each item inside the word is enough, like QQ Friend List is a ListView, each item inside has avatar, name Ah, signature what, content rich. So how do we define a content-rich item?

We need to use the adapter! Construct each ListView to have a adapter, as long as this adapter is ready, each item also will be done!

Let's take a look at the common way to construct a ListView!

New Myadapter (content,layoutid,t[]); Listview.setadapter (adpter); // first, to construct an adapter adapter,content is the current context, LayoutID is an incoming layout file that can be found through r.layout.xx, this layout file, which can be used as the layout file for item. That is, the layout of the item is the layout of the layout file, we can use this layout to set the layout of our item, t[] is an array, you can fill in the data in the item. 

An instance in which each item has a textview.

Customizing a adapter:

  

 Public classMyadapterextendsArrayadapter<string> {    //the ID of the layout file passed in    Private intResouceid;       Context context;  PublicMyadapter (Context context,intTextviewresourceid, string[] objects) {        Super(Context,textviewresourceid, objects); Resouceid=Textviewresourceid;  This. Context =context; }//This function is important and is called when each item is drawn, and the returned view is used to construct the item@Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {//This step in fact I am a bit difficult to understand, is to get into the t[] array of t[position]String City =(String) getItem (position); //Each item is a viewview view; //Set the layout for the view, which is the layout that we passed in, so that each item is the layout of the layout file that I set up.View = Layoutinflater.from (GetContext ()). Inflate (Resouceid,NULL); //find the TextView in the layoutTextView CityName =(TextView) View.findviewbyid (R.id.itemtext);        Cityname.settext (city); //returns the view used to construct the item      returnview; }}

Custom layout files:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "    android:layout_height=" Match_parent "    ><TextView        android:layout_width=" Fill_parent "        android:layout_height=" Fill_parent "        android:id=" @+id/itemtext "        android:gravity=" center "         Android:textsize= "40DP"        /></relativelayout>

Inside the OnCreate inside the ListView is set:

New Myadapter (content,layoutid,t[]); Listview.setadapter (adpter);

The whole code is relatively concise, but also relatively simple, just briefly describe the general idea. In this case, just get the item inside a textview, like a complex item as long as the control in the layout file, and then find the control inside the adapter to deal with it, the whole idea is the same.

Customizing the contents of the item inside the listview

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.