Android Study Notes (20): returning to the simple ListView

Source: Internet
Author: User

Previously, I made a step-by-step in-depth discussion on listvew. For the handheld screen, it can be relatively simple. If others are willing to pay, they don't care about the code again, this is a difference between Client development and complex server development. Of course, each person has his or her own opinions. In most cases, a list element may have a widget around each other, returning to the simple style, which is also characteristic of small screen and finger operations.

In Java, hash with <key, value> is very important for data transmission. It facilitates addition, deletion, modification, and query. If we do not use database storage, or store data in the memory. <key, value> is the most commonly used. The following example is from the current Android tutorial video (Mars-droid) on the Internet. We will focus on how to put the hashmap data into the listadapter.

Put HashMap data into ListAdapter 

In this example, <key, value> is the username and user IP address.

Step 1: Create an Android XML file

An android XML file that contains a listview and creates an android XML file that describes the horizontal layout of each listview unit. A textview on the left is used to store user_name and a textview on the right, used to store user_ip. That is, it corresponds to the key and vaule respectively.

Step 2: Initialize data in HashMap

ArrayList <HashMap <String, String>Listdata =
New ArrayList <HashMap <String, String> ();

HashMap <String, String> map1 = new HashMap <String, String> ();
HashMap <String, String> map2 = new HashMap <String, String> ();
......
Map1.put ("User_name"," James ");
Map1.put ("User_ip"," 192.168.0.1 ");
Map2.put ("user_name", "Li Si ");
Map2.put ("user_ip", "192.168.0.2 ");
......
Listdata. add (map1 );
Listdata. add (map2 );
......

Step 3: Set the ListView Adapter

SimpleAdapter listAdapter = new SimpleAdapter (this, listdata,
// 2nd parameters indicate the data source
R. layout. list_entry, // XML file describing list entry
New String [] {"user_name", "user_ip "},
// Data corresponding to user_name and user_ip in HashMap respectively
New int [] {R. id. username, R. id. userip}); // indicates the user_name and user_ip data in HaspMap and the specific widget in layout. id. username and R. id. userip.
SetListAdapter (listAdapter );

In this way, the rest of the android Study Notes (11): the most basic introduction in activity-listview. Due to the current hardware development level, including the provision of battery capabilities, the concise and clear method is better than the complicated processing, and there is no need to consider resource issues in the transition, we only need to strike a balance between program efficiency and programming art. To put it bluntly, if the program is good or not, stability or instability is the top priority of the software, and CPU capacity and power supply are the top concerns of the ODM/OEM manufacturers.

ListAdapter: CursorAdapter

In general, we can use arrayadapter to apply to many situations. There are other adapters, which are used in a similar way, But cursoradapter is somewhat different. If it is not created through newview () and BindView, use newview () and then call BindView (). If it has been created, use BindView ().

Related Links: My andriod development articles

 

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.