Android development path 08 (ListView & amp; Adapter), androidlistview

Source: Internet
Author: User

Android development path 08 (ListView & Adapter), androidlistview

ListView control Introduction: used to display data in the database or data in the network in the form of a list; ListView uses the MVC mode to separate front-end display and back-end data. That is to say, when loading data, the ListView control does not directly use ListView. add or similar methods to add data, but needs to specify an Adapter object. This object is equivalent to C (Controller) in MVC mode, and ListView is equivalent to V (View) in MVC mode, used to display data. The List or array that provides data for ListView is equivalent to the M (model) in MVC mode. In the ListView control, the data to be displayed is obtained through the Adapter object, when creating an Adapter object, you must specify the data to be displayed (List or array object). Therefore, the data to be displayed is connected to the ListView object through the Adapter object, and they are independent of each other. That is to say, listView only knows that the displayed data comes from the Adapter and does not know whether the data comes from the List or array.

1. ListView application instance (here the listview object is operated in java code ):

Public class MyListActivity extends ListActivity {

// Define a string array to represent our data source

Private static final String [] COUNTRIES = new String [] {"China", "France", "USA", "Germany", "Japan", "North Korea ", "India "};

 

@ Override

Protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

// Display the data to be presented to the ListView.

SetListAdapter (new ArrayAdapter <String> (this, R. layout. list_item, COUNTRIES ));

// Obtain the listview object through the getListView () method

ListView lv = getListView ();

Lv. setTextFilterEnabled (true );

Log. e ("MyListActivity", "generated on the listview interface ");

Lv. setOnItemClickListener (new OnItemClickListener (){

 

@ Override

Public void onItemClick (AdapterView <?> Parent, View view,

Int position, long id ){

// Use the getText () method to obtain the options in the selected ListView.

Log. e ("MyListActivity", "The list option is clicked to see what will appear ");

Toast. makeText (getApplicationContext (), (TextView) view). getText (), Toast. LENGTH_LONG). show ();

}

 

});

}

}

The xml file of the list item 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 = "fill_parent"

Android: padding = "10dp"

Android: textSize = "16sp">

</TextView>

 

Java code ①:

Public class MyActivity extends Activity {

Private ListView listview;

Private ArrayAdapter <String> adapter;

// Define a data source

Private List <String> data = null;

@ Override

Protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. activity_main );

// Obtain the data source

Data = MyDataSource. getDataSource ();

Adapter = new ArrayAdapter <String> (this, R. layout. list_item, data );

Listview = (ListView) findViewById (R. id. listview );

// Display the data source to the listview view through the adapter

Listview. setAdapter (adapter );

 

 

}

}

Java code ②:

 

Public class MyDataSource {

 

Public MyDataSource (){

Super ();

}

 

Public static List <String> getDataSource (){

List <String> list = new ArrayList <String> ();

List. add ("Beijing ");

List. add ("Shanghai ");

List. add ("Guangzhou ");

List. add ("Hubei ");

List. add ("Hunan ");

List. add ("Shenzhen ");

List. add ("Xi'an ");

 

Return list;

 

}

 

}

 

Layout file activity_main.xml code:

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"

Android: layout_width = "match_parent"

Android: layout_height = "match_parent">

 

<ListView

Android: id = "@ + id/listview"

Android: layout_width = "match_parent"

Android: layout_height = "match_parent"

Android: text = "@ string/hello_world"/>

 

</RelativeLayout>

 

Code of list_item.xml:

<? 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 = "fill_parent"

Android: padding = "10dp"

Android: textSize = "16sp">

</TextView>

 

2. ListView application instance:

In the above two examples, we use ArrayAdapter (array adapter), which shows a single data source, while SimpleAdapter can define various la S and put ImageView (image ), you can also put buttons and CheckBox;

Next let's take a look at how to use SimpleAdapter

① MainActivity. java

Public class MainActivity extends Activity {

Private ListView mListView;

Private SimpleAdapter mSimpleAdapter = null;

Private List <Map <String, Object> data = null;

@ Override

Protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. activity_main );

MListView = (ListView) findViewById (R. id. listview );

Data = new ArrayList <Map <String, Object> ();

/**

* SetAdapter method:

* The first parameter is a Context object;

* Second parameter: A set in the form of List <Map <String, Object>. Why?

* List <E> is used to store List items in listview. Each element in the List <E> set has its own features.

* As a result, we use a map set to represent the list elements to achieve diversified display of listview data;

* Third parameter: it is a layout file used to display the content of each list item of listview.

* Fourth parameter: it is the array representation of the key of Map in List <Map <String, Object>;

* Fifth parameter: represents the array of Map values in List <Map <String, Object>;

*/

Map <String, Object> mMap1 = new HashMap <String, Object> ();

MMap1.put ("title", "I am the first feature ");

MMap1.put ("icon", R. drawable. ic_launcher );

Map <String, Object> mMap2 = new HashMap <String, Object> ();

MMap2.put ("title", "I am the second feature ");

MMap2.put ("icon", R. drawable. ic_launcher );

Map <String, Object> mMap3 = new HashMap <String, Object> ();

MMap3.put ("title", "I am the third feature ");

MMap3.put ("icon", R. drawable. ic_launcher );

Data. add (mMap1 );

Data. add (mMap2 );

Data. add (mMap3 );

MListView. setAdapter (new SimpleAdapter (this, data, R. layout. list_item,

New String [] {"title", "icon"}, new int [] {R. id. TV, R. id. iv }));

}

}

 

② Activity_main.xml

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"

Android: layout_width = "match_parent"

Android: layout_height = "match_parent">

 

<ListView

Android: id = "@ + id/listview"

Android: layout_width = "match_parent"

Android: layout_height = "match_parent"/>

 

</RelativeLayout>

③ List_item.xml

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"

Android: layout_width = "match_parent"

Android: layout_height = "match_parent">

 

<ImageView

Android: id = "@ + id/iv"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"/>

<TextView

Android: id = "@ + id/TV"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"/>

</LinearLayout>

 

3. Custom adapter: When the android interface sometimes needs to display a slightly complex interface, we need to define an adapter ourselves, so we need to inherit the BaseAdapter.

 

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.