Android custom listview

Source: Internet
Author: User

Here I will explain the custom listview. Let's go straight to the real question. First, I will create a new project: listdemo;

Next, we need to create an XML file. Here I will write it directly in Main. xml:

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Listview
Android: Id = "@ + ID/listview"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
</Linearlayout>
Then we need to define an item. XML to display its data:

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "horizontal"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Imageview
Android: Id = "@ + ID/images_id"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/icon"
/>
<Linearlayout
Android: Orientation = "vertical"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
>
<Textview
Android: Id = "@ + ID/text_id"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "default_001"
/>
<Textview
Android: Id = "@ + ID/text_info"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "default_info"
/>
<Textview
Android: Id = "@ + ID/text_url"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "default_url"
/>

</Linearlayout>
</Linearlayout>

Now we have completed the XML file, that is, to define the layout of the adapted data. First, we need to create a class:

Package com. JSD;

Import java. util. arraylist;

Import Android. App. activity;
Import Android. content. context;
Import Android. OS. Bundle;
Import Android. widget. listview;

Import com. JSD. Adapter. appadapter;
Import com. JSD. model. appinfos;

/**
* Listview customer
* @ Author jiangshide
*
*/
Public class demo extends activity {
 
Private context mcontext;
Private listview;
Private arraylist <appinfos> appinfos;
Private appadapter;
 
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Getcomponenets ();
}
 
/**
* Componenet
*/
Public void getcomponenets (){
This. mcontext = this;
Listview = (listview) findviewbyid (R. Id. listview );
Appinfos = new arraylist <appinfos> ();
Loaddatas ();
Appadapter = new appadapter (mcontext, appinfos );
Listview. setadapter (appadapter );
}
 
/**
* Load datas
*/
Public void loaddatas (){
For (INT I = 0; I <10; I ++ ){
Appinfos AI = new appinfos ();
If (I = 1 ){
AI. setimages (R. drawable. Icon );
} Else if (I = 2 ){
AI. setimages (R. drawable. Item1 );
} Else if (I = 3 ){
AI. setimages (R. drawable. item2 );
} Else if (I = 4 ){
AI. setimages (R. drawable. item3 );
} Else if (I = 5 ){
AI. setimages (R. drawable. item4 );
} Else if (I = 6 ){
AI. setimages (R. drawable. item5 );
}
AI. settext_id ("001 ");
AI. settext_infos ("this is what we need to pay attention ...");
AI. settext_url ("http://jiangshide.com ");
Appinfos. Add (AI );
}
}
}

Ui class of the main thread,

In fact, you need to wear an adapter to inherit the baseadapter:

Package com. JSD. Adapter;

Import java. util. arraylist;

Import Android. content. context;
Import Android. View. view;
Import Android. View. viewgroup;
Import Android. widget. baseadapter;

Import com. JSD. model. appinfos;
Import com. JSD. View. appview;

/**
* Adapter
* @ Author jiangshide
*
*/
Public class appadapter extends baseadapter {

Private context mcontext;
Private arraylist <appinfos> appinfos;
 
Public appadapter (context c ){
This. mcontext = C;
}
 
Public appadapter (context c, arraylist <appinfos> appinfos ){
This. mcontext = C;
This. appinfos = appinfos;
}
 
Public int getcount (){
Return appinfos. Size ();
}

Public object getitem (INT position ){
Return appinfos. Get (position );
}

Public long getitemid (INT position ){
Return position;
}

/**
* Get View
*/
Public View getview (INT position, view convertview, viewgroup parent ){
If (appinfos = NULL ){
Return NULL;
}
If (convertview = NULL ){
Appview = new appview (mcontext );
Appview. updateview (appinfos. Get (position ));
Convertview = appview;
} Else {
(Appview) convertview). updateview (appinfos. Get (position ));
}
Return convertview;
}

}

In order to adapt the data, we need to persist the data: Save the data in bean mode.

Package com. JSD. model;

/**
* Datas
* @ Author jiangshide
*
*/
Public class appinfos {

Private int images;
Private string text_id;
Private string text_infos;
Private string text_url;
 
Public int getimages (){
Return images;
}
Public void setimages (INT images ){
This. Images = images;
}
Public String gettext_id (){
Return text_id;
}
Public void settext_id (string textid ){
Text_id = textid;
}
Public String gettext_infos (){
Return text_infos;
}
Public void settext_infos (string textinfos ){
Text_infos = textinfos;
}
Public String gettext_url (){
Return text_url;
}
Public void settext_url (string texturl ){
Text_url = texturl;
}
}

The second step is to define the control of the View display layout to obtain data parameters.

Package com. JSD. view;

Import Android. content. context;
Import Android. View. layoutinflater;
Import Android. View. view;
Import Android. widget. imageview;
Import Android. widget. linearlayout;
Import Android. widget. textview;

Import com. JSD. R;
Import com. JSD. model. appinfos;

/**
* View
* @ Author jiangshide
*
*/
Public class appview extends linearlayout {

Private context mcontext;
Private imageview image_id;
Private textview text_id, text_info, text_url;
 
/**
*
* @ Param Context
*/
Public appview (context ){
Super (context );
This. mcontext = context;
Initilize (context );
}
 
/**
*
* @ Param C
*/
Public void initilize (context c ){
This. mcontext = C;
View view = layoutinflater. From (this. mcontext). Inflate (R. layout. Item, null );
Image_id = (imageview) view. findviewbyid (R. Id. images_id );
Text_id = (textview) view. findviewbyid (R. Id. text_id );
Text_info = (textview) view. findviewbyid (R. Id. text_info );
Text_url = (textview) view. findviewbyid (R. Id. text_url );
Addview (View );
}
 
/**
*
* @ Param AI
*/
Public void updateview (appinfos AI ){
Image_id.setimageresource (AI. getimages ());
Text_id.settext (AI. gettext_id ());
Text_info.settext (AI. gettext_infos ());
Text_url.settext (AI. gettext_url ());
}
}

OK. The above is the complete code for implementing the custom implementation of listview,

Next, let's talk about the custom display mode of the gridview, which will also be implemented in the complete code display mode ......

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.