For notes about how SimpleAdapter and ListView work together to implement the List View, simpleadapter

Source: Internet
Author: User

For notes about how SimpleAdapter and ListView work together to implement the List View, simpleadapter

To use ListView, you need to add an adapter for it:

There are two types of adapters: 1. ArrayAdapter -- used for separate text display

2. SimpleAdapter -- used for text and Image Display

SimpleAdapter:

First look at the constructor of SimpleAdapter:

SimpleAdapter (context, data, resource, from,)

-- Context: context refers to the activity itself.

-- Data: data source: A List set containing map; each element in the List is a Map set, and Map contains multiple key-value pairs.

-- Resource: Layout file, which can be written by yourself. It can be obtained under R. Layout. The sum of the corresponding elements in the Layout can be stored in the Map.

-- From: a String [] array, which corresponds to the key element in the Map, similar to the initial name.

-- To: obtain the value of the corresponding key from the Map and store the specific value.

The following is an example.

Tool: Android studio

First, write the layout file: item. xml, which is stored in the layou folder.

  

<?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/itempic"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="15dp"        android:src="@mipmap/ic_launcher" />    <TextView        android:id="@+id/itemtext"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="demo"        android:textSize="40sp" /></LinearLayout>

 

The instance in MainActivity receives ListView and SimpleAdapter objects.

Private ListView listView; // private ArrayAdapter <String> adapter; private SimpleAdapter si_adapter; // the list set is an abstract set that instantiates its instance object ArrayList private List <Map <String, object> simpleData; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); listView = (ListView) findViewById (R. id. listView); simpleData = new ArrayList <Map <String, Object> (); si_adapter = new SimpleAdapter (this, getData (), R. layout. item, new String [] {"pic", "text"}, new int [] {R. id. itempic, R. id. itemtext}); listView. setAdapter (si_adapter );}

The above getdate () method:

Public List <Map <String, Object> getData () {for (int I = 0; I <20; I ++) {Map <String, object> map = new HashMap <String, Object> ();
// Fill in the actual pic information map. put ("pic", R. mipmap. ic_launcher );
// Fill in the actual text information map. put ("text", "robot" + I + "); simpleData. add (map);} return simpleData ;}

Add the SimpleAdapter adapter to the ListView.

Analysis: SimpleAdapter's data parsing

Layout file: determines how data is presented in a view.

From: Specifies a specific symbol for each element.

To: The corresponding situation of each element in XML

Data: data source, used for parsing

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.