In the previous continuous listvew made a gradual in-depth discussion, for handheld screen, in fact, can be relatively simple, if others are willing to pay, do not care about the code again, this is the development of clients and complex server development of different places. Of course, everyone has their own views. Most of the cases, a list element may have a widget about the same, return to the minimalist style, which is the small size of the screen and finger operation features.
In the data transfer, Java inside, with <Key,Value> hash is very important, can be convenient to increase/delete/change/check, if we do not use database storage, or store data in memory,<key,value> is the most common. The following example is from an Android tutorial video currently online (mars-droid), and we will focus on how to put HashMap data into ListAdapter.
HashMap data into the ListAdapter
In this example <key,value> is the user name and User IP.
Step 1: Build an android XML file
An android XML, which contains a ListView, builds an Android XML file that describes the layout of the horizontal layouts of each ListView unit, and the left is a textview for storing user_name, A textview on the right for storing user_ip. That is, the key and Vaule are respectively corresponding.
Step 2: Initialize the data in the HashMap
arraylistListdata =new Arraylist
hashmap<string,string> map1 = new hashmap<string,string> ();
hashmap<string,string> map2 = new hashmap<string,string> ();
... ...
Map1.put ("user_name"," Zhang San ");
Map1.put ("user_ip"," 192.168.0.1 ");
Map2.put ("user_name", "John Doe");
Map2.put ("User_ip", "192.168.0.2");
... ...
Listdata.add (MAP1);
Listdata.add (MAP2);
... ...
Step 3: Set up the adapter for the ListView
Simpleadapter listadapter = new Simpleadapter (this, listdata,//2nd parameter means data source
R.layout.list_entry,//corresponds to the XML file that describes the list entry
New string[]{"user_name", "USER_IP"},//corresponding to user_name in HashMap, and USER_IP data
New Int[]{r.id.username,r.id.userip}); This indicates that the user_name and USER_IP data in the above Haspmap are convenient and correspond to the specific widget,r.id.username and R.id.userip in layout.
Setlistadapter (ListAdapter);
That's it, the rest of the study notes by Android (11): The most basic introduction to Activity-listview. Because of the current level of hardware development, including the provision of battery capacity, concise and clear way, compared to those complicated processing is better, without the transition to consider the resource problem, we only need to be in the program efficiency and programming art to strike a balance. Plainly the procedure is good, stability instability is the first problem of the software, CPU power and power supply is the first odm/oem manufacturers.
Listadapter:cursoradapter
Generally speaking, we can use Arrayadapter to apply a lot of cases, there are other adapter, using similar, but cursoradapter some different, through Newview () and BindView (), if not created, Use Newview (), and then call BindView (), if it is already created, use BindView ().
RELATED links: My Andriod development related articles
Android Learning Note (20): Return to Simple ListView