Simple use of Baseadapter in ListView

Source: Internet
Author: User

This article is mainly to write down the simple implementation of the ListView. (using Baseadapter)

Take a little note after your own understanding and add more comments to the Baseadapter section.

Also hope to give me the same novice to some help.

The following are the implementation effects:

Next is the code:

Main.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= "Vertical" >

<listview
Android:id= "@+id/lv"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:fastscrollenabled= "true"
/>

</LinearLayout>

List_item.xml

<?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"
/>
<linearlayout
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:orientation= "Vertical"
>
<textview
Android:id= "@+id/tv"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:textsize= "20SP"
/>
<textview
Android:id= "@+id/info"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:textsize= "14SP"
/>
</LinearLayout>

</LinearLayout>

Demo17activity.java

Package com.example.baseadapter1;

Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;

Import android.app.Activity;
Import Android.content.Context;
Import Android.os.Bundle;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.ImageView;
Import Android.widget.ListView;
Import Android.widget.TextView;

public class Demo17activity extends Activity {
Private ListView LV;
Private list<map<string,object>> data;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

lv= (ListView) Findviewbyid (r.id.lv);
Data=getdata ();
Myadapter adapter=new Myadapter (this);
Lv.setadapter (adapter);

}

Private List<map<string,object>> GetData ()
{
List<map<string,object>> list=new arraylist<map<string,object>> ();
for (int i=0;i<10;i++)
{
Map<string,object> map=new hashmap<string, object> ();
Map.put ("img", R.drawable.ic_launcher);
Map.put ("title", "Hehehehehe");
Map.put ("info", "66666666");
List.add (map);
}
return list;
}

Static Class Viewholder
{
Public ImageView img;
Public TextView title;
public TextView info;
}

public class Myadapter extendsBaseadapter{
Private Layoutinflater Minflater=null;

PublicMyadapter(Context context) {
TODO auto-generated Constructor stub
This.minflater=layoutinflater.from (context);//This is to determine which layout of your ListView is displayed
}

@Override
public intGetCount (){
TODO auto-generated Method Stub
return Data.size ();//This determines how many item you have in a ListView
}
@Override
Public ObjectGetItem(int position) {
TODO auto-generated Method Stub
return position;
}
@Override
Public longGetitemid(int position) {
TODO auto-generated Method Stub
return position;
}
@Override
Public ViewGetView(int position, View Convertview, ViewGroup parent) {
TODO auto-generated Method Stub
Viewholder Holder=null;
if (convertview==null)
{
Holder=new Viewholder ();
Convertview=minflater.inflate (R.layout.list_item,null);//This determines the layout of each item in your ListView
Holder.img = (ImageView) Convertview.findviewbyid (r.id.img);//Here is the binding of the content to the control.

Holder.title = (TextView) Convertview.findviewbyid (r.id.tv); //Note: Add Convertview before Findviewbyid here.
Holder.info = (TextView) Convertview.findviewbyid (r.id.info);
Convertview.settag (holder);
}
else{
Holder= (Viewholder) Convertview.gettag (); //This is to improve the operation efficiency of the ListView
}
Holder.img.setBackgroundResource ((Integer) data.get (position). Get ("img"));
Holder.title.setText (String) data.get (position). Get ("title"));
Holder.info.setText (String) data.get (position). Get ("info"));

return convertview;
}
}

}

Simple use of Baseadapter in ListView

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.