Use of ListView and adapter

Source: Internet
Author: User

First, the use of Arrayadapter

There are several constructors for Arrayadapter, where resource refers to the layout file for each list item, and objects refers to the data source of the list item, which is usually referred to as an array

Arrayadapter (context context, int resource)

Arrayadapter (context context, int resource, int textviewresourceid)

Arrayadapter (context context, int resource, t[] objects)

Arrayadapter (context context, int resource, int textviewresourceid, t[] objects)

) ">arrayadapter (context context, int resource, list<t> objects)

) ">arrayadapter (context context, int resource, int textviewresourceid, list<t> objects)

Public constructors Public Arrayadapter (context context, int resource)

Added in API Level 1

Constructor

Parameters

Context
The current context.

Resource
The resource ID for a layout file containing a TextView to use when instantiating views.

Public Arrayadapter (context context, int resource, int textviewresourceid)

Added in API Level 1

Constructor

Parameters

Context
The current context.

Resource
The resource ID for a layout file containing a layout to use when instantiating views.

Textviewresourceid
The ID of the TextView within the layout resource to be populated

Public Arrayadapter (context context, int resource, t[] objects)

Added in API Level 1

Constructor

Parameters

Context
The current context.

Resource
The resource ID for a layout file containing a TextView to use when instantiating views.

Objects
The objects to represent in the ListView.

Public Arrayadapter (context context, int resource, int textviewresourceid, t[] objects)

Added in API Level 1

Constructor

Parameters

Context
The current context.

Resource
The resource ID for a layout file containing a layout to use when instantiating views.

Textviewresourceid
The ID of the TextView within the layout resource to be populated

Objects
The objects to represent in the ListView.

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

Added in API Level 1

Constructor

Parameters

Context
The current context.

Resource
The resource ID for a layout file containing a TextView to use when instantiating views.

Objects
The objects to represent in the ListView.

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

Added in API Level 1

Constructor

Parameters

Context
The current context.

Resource
The resource ID for a layout file containing a layout to use when instantiating views.

Textviewresourceid
The ID of the TextView within the layout resource to be populated

Objects
The objects to represent in the ListView.

1.mainactivity.java

Package org.crazyit.ui;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.widget.ArrayAdapter;
Import Android.widget.ListView;


public class Mainactivity extends Activity
{
@Override
protected void OnCreate (Bundle savedinstancestate)
{
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
ListView List1 = (ListView) Findviewbyid (R.ID.LIST1);
Define an array
String[] arr1 = {"Monkey King", "Pig", "Ox Demon King"};
Wrap the array as Arrayadapter
arrayadapter<string> Adapter1 = new arrayadapter<string>
(This, R.layout.array_item, arr1);
Set adapter for the ListView
List1.setadapter (Adapter1);
ListView List2 = (ListView) Findviewbyid (R.ID.LIST2);
Define an array
String[] arr2 = {"Java", "Hibernate", "Spring", "Android"};
Wrap the array as Arrayadapter
arrayadapter<string> adapter2 = new arrayadapter<string>
(This, R.layout.checked_item, arr2);
Set adapter for the ListView
List2.setadapter (ADAPTER2);
}
}
2.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= "Match_parent"
android:layout_height= "Match_parent" >
<!--settings use a red divider--
<listview
Android:id= "@+id/list1"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:divider= "#f00"
android:dividerheight= "2px"
Android:headerdividersenabled= "false"/>
<!--settings use a green divider--
<listview
Android:id= "@+id/list2"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:divider= "#0f0"
android:dividerheight= "2px"
Android:headerdividersenabled= "false"/>
</LinearLayout>
3.array_item.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<textview
Xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/textview"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:textsize= "24DP"
Android:padding= "10px"
Android:shadowcolor= "#f0f"
android:shadowdx= "4"
Android:shadowdy= "4"
android:shadowradius= "2"/>
4.checked_item.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<checkedtextview
Xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/textview"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:textsize= "24DP"
android:checkmark= "@drawable/ok"
Android:shadowcolor= "#f0f"
android:shadowdx= "4"
Android:shadowdy= "4"
android:shadowradius= "2"/>
The constructor for using Simpleadapersimpleadapter is as follows, where resource is the layout resource for the list item, and to the id;data of each column in the layout resource is the data source for that list item, where each list item of data contains a multicolumn map &LT;STRING,?&GT; Each map is a column, and each key value in the map is indicated in the from. Public Simpleadapter (context context, list<?. extends Map<string,?>> data, int resource, string[] from, int[] To)

Added in API Level 1

Constructor

Parameters

Context
The context where the View associated with this simpleadapter is running

Data
A List of Maps. Each entry in the list corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in ' from '

Resource
Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in ' to '

From
A List of column names that would be added to the MAP associated with each item.

To
The should display column in the ' from ' parameter. These should all is textviews. The first n views in this list is given the values of the first n columns in the from parameter.

1.mainactivity.java

Package org.crazyit.ui;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.widget.AdapterView;
Import static Android.widget.AdapterView.OnItemClickListener;
Import static Android.widget.AdapterView.OnItemSelectedListener;
Import Android.widget.ListView;
Import Android.widget.SimpleAdapter;

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


public class Mainactivity extends Activity
{
Private string[] names = new string[]
{"Tiger Head", "Get Jade", "Li Qingzhao", "Li Bai"};
Private string[] Descs = new string[]
{"Lovely child", "a girl who is good at music"
, "A woman who specializes in literature", "Romantic Poet"};
Private int[] Imageids = new int[]
{R.drawable.tiger, R.drawable.nongyu
, R.drawable.qingzhao, R.drawable.libai};
@Override
public void OnCreate (Bundle savedinstancestate)
{
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Create a list collection with the elements of the list set as map
list<map<string, object>> listItems =
New arraylist<map<string, object>> ();
for (int i = 0; i < names.length; i++)
{
map<string, object> listItem = new hashmap<string, object> ();
Listitem.put ("header", Imageids[i]);
Listitem.put ("PersonName", Names[i]);
Listitem.put ("desc", Descs[i]);
Listitems.add (ListItem);
}
Create a Simpleadapter
Simpleadapter simpleadapter = new Simpleadapter (this, ListItems,
R.layout.simple_item,
New string[] {"PersonName", "header", "Desc"},
New int[] {r.id.name, R.id.header, R.id.desc});
ListView list = (ListView) Findviewbyid (R.id.mylist);
Set adapter for the ListView
List.setadapter (Simpleadapter);

Event Listener binding for the Click event of a ListView list item
List.setonitemclicklistener (New Onitemclicklistener ()
{
This method is fired when item position is clicked
@Override
public void Onitemclick (adapterview<?> parent, view view,
int position, long ID)
{
System.out.println (Names[position]
+ "clicked");
}
});
To bind event listeners to the selected event for a ListView list item
List.setonitemselectedlistener (New Onitemselectedlistener ()
{
The method is fired when item position is selected
@Override
public void onitemselected (adapterview<?> parent, view view,
int position, long ID)
{
System.out.println (Names[position]
+ "selected");
}
@Override
public void onnothingselected (adapterview<?> parent)
{
}
});

}
}

2.main.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Horizontal"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content" >
<!--define a ListView--
<listview android:id= "@+id/mylist"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"/>
</LinearLayout>
3.simple_item.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Horizontal"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content" >
<!--defines a imageview that is used as part of a list item. -
<imageview android:id= "@+id/header"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:paddingleft= "10DP"/>
<linearlayout
android:orientation= "Vertical"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content" >
<!--defines a textview that is used as part of a list item. -
<textview android:id= "@+id/name"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:textsize= "20DP"
Android:textcolor= "#f0f"
android:paddingleft= "10DP"/>
<!--defines a textview that is used as part of a list item. -
<textview android:id= "@+id/desc"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:textsize= "14DP"
android:paddingleft= "10DP"/>
</LinearLayout>
</LinearLayout>
Third, the use of Baseadapter

Using Baseadapter, the most important time to rewrite the methods that are provided, and the layout files are implemented through their getview.

Package org.crazyit.ui;

Import android.app.Activity;
Import Android.graphics.Color;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.ImageView;
Import Android.widget.LinearLayout;
Import Android.widget.ListView;
Import Android.widget.TextView;


public class Mainactivity extends Activity
{
ListView myList;
@Override
public void OnCreate (Bundle savedinstancestate)
{
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
MyList = (ListView) Findviewbyid (r.id.mylist);
Baseadapter adapter = new Baseadapter ()
{
@Override
public int GetCount ()
{
Specify a total of 40 options
return 10;
}
@Override
Public Object getItem (int position)
{
return null;
}
Override this method, and the return value of the method will be the ID of the list item
@Override
public long getitemid (int position)
{
return position;
}
Override this method, and the view returned by the method will be used as a list box
@Override
Public View GetView (int position
, View Convertview, ViewGroup parent)
{
Create a linearlayout and add two components to it
LinearLayout line = new LinearLayout (mainactivity.this);
Line.setorientation (0);
ImageView image = new ImageView (mainactivity.this);
Image.setimageresource (R.drawable.ic_launcher);
TextView Text = new TextView (mainactivity.this);
Text.settext ("No." + (position +1) + "list item");
Text.settextsize (20);
Text.settextcolor (color.red);
Line.addview (image);
Line.addview (text);
Returning LinearLayout instances
return line;
}
};
Mylist.setadapter (adapter);
}
}
2.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= "Match_parent"
android:layout_height= "Match_parent" >
<listview
Android:id= "@+id/mylist"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"/>
</LinearLayout>

Use of ListView and adapter

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.