Android's ListView related

Source: Internet
Author: User

A ListView is a list view that displays the various controls provided by ListAdapter in a vertical and scrollable list. Note that you create the adapter and set it to the ListView.

1.ArrayAdapter

The Arrayadapter is constructed from 3 parameters, the first is the context, the second is the layout defined in the R file, the system's R file is available, and the third parameter is an array with no restrictions on the type of each item in the array.

The default layout of the system is available via Android. r.layout.xx definition.

private static string[] data={"A", "B", "C", "D"};
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

ListView listview=new ListView (This);
Arrayadapter adapter=new arrayadapter<string> (this, Android. R.layout.simple_list_item_1,data);
Listview.setadapter (adapter);
Setcontentview (listview);
}

If you customize each item in the ListView, the style of TextView Arraylayout.xml is as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<textview xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content"
android:gravity= "Center_horizontal"/>

Activity, specify arrayadapter the second parameter is arraylayout.xml:

private static string[] data={"A", "B", "C", "D"};
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

ListView listview=new ListView (This);
Arrayadapter adapter=new arrayadapter<string> (this,r.layout.arraylayout, data);
Listview.setadapter (adapter);
Setcontentview (listview);
}

2 Simpleadapter

Each item in the Simpleadapter ArrayList is a map<string,?> type, and each map object corresponds to a data-bound one by one in Listv.

Private ListView ListView;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Listview=new ListView (this);
Data2 = new arraylist<map<string, object>> ();
Map<string, object> item;
item = new hashmap<string, object> ();
Item.put ("name", "Zhang San");
Item.put ("Gender", "male");
Item.put ("Age", "25");
Data2.add (item);
item = new hashmap<string, object> ();
Item.put ("name", "John Doe");
Item.put ("Gender", "male");
Item.put ("Age", "33");
Data2.add (item);
item = new hashmap<string, object> ();
Item.put ("name", "Xiao Wang");
Item.put ("Gender", "female");
Item.put ("Age", "31");
Data2.add (item);
Simpleadapter adapter =New Simpleadapter (This, data2,
R.layout.simplelayout, new string[] {"Name", "Gender", "Age"}, new int[] {
R.ID.TV01, r.id.tv02,r.id.tv03});
Listview.setadapter (adapter);
Setcontentview (listview);

The layout file for each item in the ListView is as follows:

<?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" >
<textview android:id= "@+id/tv01" android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" android:width= "150DP"/>
<textview android:id= "@+id/tv02" android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" android:width= "150DP"/>
<textview android:id= "@+id/tv03" android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" android:width= "150DP"/>
</LinearLayout>

If you set the activity's layout file to contain not only the ListView, as follows:

<?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" >
<imageview android:layout_width= "fill_parent" android:layout_height= "Wrap_content"
android:src= "@drawable/img01"/>
<ListView android:id= "@+id/listview01" android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content" android:choicemode= "Singlechoice"/>
</LinearLayout>

In the activity:

Setcontentview (R.layout.main);
listview= (ListView) Findviewbyid (R.ID.LISTVIEW01);
Listview.setadapter (adapter);

3 Baseadapter

public class Mainactivity extends Activity {
/** called when the activity is first created. */
int []Drawableids={R.DRAWABLE.IMG01,R.DRAWABLE.IMG02,R.DRAWABLE.IMG03};
int []Msgids={R.STRING.STR1,R.STRING.STR2,R.STRING.STR3};
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);

Setcontentview (R.layout.main);
ListviewListView= (ListView) Findviewbyid (R.ID.LISTVIEW01);
Baseadapter ba=newBaseadapter() {

@Override
PublicView GetView(int position, View Convertview, ViewGroup parent) {
TODO auto-generated Method Stub
LinearLayout ll=new LinearLayout (mainactivity.this);
Ll.setorientation (linearlayout.horizontal);
Ll.setpadding (5, 5, 5, 5);
ImageView ii=new ImageView (mainactivity.this);
Ii.setimagedrawable (Getresources (). getdrawable (Drawableids[position]));
Ii.setscaletype (ImageView.ScaleType.FIT_XY);
Ii.setlayoutparams (New Gallery.layoutparams (50,50));
Ll.addview (ii);
TextView tv=new TextView (mainactivity.this);
Tv.settext (Getresources (). GetText (Msgids[position]));
Tv.settextsize (24);
Tv.settextcolor (MainActivity.this.getResources (). GetColor (R.color.white));
Tv.setpadding (5, 5, 5, 5);
Tv.setgravity (Gravity.left);
Ll.addview (TV);
Returnll;
}

@Override
public long getitemid (int position) {
TODO auto-generated Method Stub
return 0;
}

@Override
Public Object getItem (int position) {
TODO auto-generated Method Stub
return null;
}

@Override
public int GetCount () {
TODO auto-generated Method Stub
return 3;
}
};
listview.setadapter (BA);
}
}

4 listactivity

If you use Listactivity, the ListView in the activity will fill the screen.

In a layout file, you must define a ListView whose ID is @id/android:list, and another that needs to be defined but not necessarily a textview with an ID of @id/android:empty, which is what is displayed when there is no data in the ListView.

<?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" >
<listview android:id= "@id/android:list" android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"/>
<textview android:id= "@id/android:empty" android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" android:text= "no Data"/>
</LinearLayout>

Activity, the ListView is set to the same row as the previous method.

String [] data={"A", "B", "C", "D"};
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);

Setcontentview (R.layout.main);
Setlistadapter (New arrayadapter<string> (this, Android. R.layout.simple_list_item_1,data));

}

Android's ListView related

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.