Android: ListView use (list controls)

When the program has a lot of data needs to show, you need to use ListView friends. ListView allows the user to scroll through the data into the screen outside the screen by sliding the finger upward and downward, while the original data on the screen is scrolled off the screen.

1. Basic properties

(1) Since the data in the array can not be passed directly to the ListView, we need the help of an adapter (Adapter) to complete.
There are a variety of (2) Adapter, select a different adapter depending ListView usage. There are two main ArrayAdapter / SimpleAdapter

ListView a key operation is made to meet the requirements of the Adapter, in the data injection ListView.

2.ArrayAdapter / SimpleAdapter

  ArrayAdapter adapter is an array adapter, the adapter can only go to show a very simple pattern, can only show a control of entry Item.

  SimpleAdapter adapter is a handy adapter, you can show some ArrayAdapter complex than the pattern, can show more than one entry controls the Item.

Both parameters created different:

(1) ArrayAdapter <data type> adapter = new ArrayAdapter <data type> (context: Activity object generally refers to the current, layout: each list entry (item) layout, data display: a data source variable);

(2) SimpleAdapter adapter = new SimpleAdapter (context: generally refers to the current Activity object, data: to be implanted in the data source, layout: each list entry (item) displayed layout, new String [] {}: data source " key ", new int [] {}: display control data source ID);

among them:

To organize your data in the form of matching, data ArrayAdapter the form of an array, in the form of SimpleAdapter is ArrayList <HashMap <String, Object >>.

SimpleAdapter last two parameters, new String [] means that the key has been built hashmap corresponding to the data fetch, into new int [] corresponding controls.

ArrayAdapter:

    Private String [] = {Data " the Sunny " , " Cloudy " , " Few Clouds " , " Mostly Cloudy " , " Overcast " , " Windy " , " Calm " , " Light Breeze " };
         // be designated by the generic adapted to the type of data, then the constructor adapted incoming data.
        // android.R.layout.simple_list_item_1 is a built-in child ListView layout, the TextView there is only one, can display a text
         // Data represents the data to be adapted 
        ArrayAdapter <String>The ArrayAdapter <String> (the MainActivity. The this , android.R.layout.simple_list_item_1, Data); 
        the ListView listView = (the ListView) the findViewById (R.id.list_view);
         // The constructed objects pass into the adapter 
        listView.setAdapter (adapter );

SimpleAdapter:

Private String [] = {the Detail , " " boiled "is a Cantonese cooking techniques is to use boiling water or soup boiled food. Most northerners are on the" boiled "There is a conceptual confusion that boiled it is to use white boiled fish out, it is not true, it is a misunderstanding of the literal, real boiled shrimp still has a few steps, through a friend's advice, was able epiphany. boiled shrimp is the most simple, without too much spices, no complicated process, do not have to spend a lot of time, even without considering the temperature of the food, not too much to contemplate wobble plate decorated with an equally simple little sour salty Weidie, you can serve a particular want to eat them too tired and too lazy person, you can choose to do this dish, simple and fresh. my favorite is a recipe retained as a shrimp, clothes neat appearance respectable complete, it is undoubtedly the highest reward , gentleman and elegantly enjoyed humans of course is the best outcome. " ,
             " according to legend, the United States and roast duck, is derived from rare species of Beijing duck, it is the world's best A duck meat is said that this special pure Beijing duck breeding, about starting in about a thousand years ago, because of the Liao, Jin and Yuan emperors hunting, even given this kind of white ducks, after the safari and support, has been inherited only was this an excellent pedigree, and developed into the meat of this precious species of duck. that is, a white duck fattening by gavage method, named "duck." Moreover, Beijing duck there had been a hundred years ago in to Europe, the breeding blockbuster thus, as a high-quality varieties of Beijing duck, duck become the world's rare species has long been the source.. " 
    }; 
    Private String [] {title = " boiled shrimp " , " Beijing duck " };
    private int[] imageid={R.drawable.pic1,R.drawable.pic2};
    List<HashMap<String, Object>> data;
   
  data
=new ArrayList<HashMap<String, Object>>(); for(int i=0;i<title.length;i++){ HashMap<String,Object> showitem = new HashMap<String,Object>(); showitem.put("name",title[i]); showitem.put("says",detail[i]); showitem.put("img",imageid[i]); data.add(showitem); } //创建SimpleAdapter SimpleAdapter adapter=new SimpleAdapter(getApplicationContext(),data,R.layout.list_item,new String[]{"name","says","img"},new int[]{R.id.text1,R.id.text2,R.id.img}); lstmenu.setAdapter(adapter);

Here is other people's code, an explanation of each parameter:

        // Use SimpleAdapter adapter 
        ListAdapter ListAdapter = new new SimpleAdapter ( the this , // Context 
                                  List,          // data 
                                  android.R.layout.simple_list_item_2, // use the system layout new new String [] { " ID " , " name " }, / / set the Map key, the data come from new new int [] {android.R.id.text1, android.R.id.text2}); // two ID control system layout, the data provided to go there // the adapter to the ListView 
        listView.setAdapter (ListAdapter);
                
                                  
                                   

        

supplement:

It has provided a very good frame -MVC frame in J2EE implementation principle: data model M (Model) data stored by the controller C (Controller) The data displayed on the view V (View). There is a high level controls in Android, he implementation process is similar to the MVC framework. The reason that it advanced because of his use, unlike other controls, to be able to use drag and drop interface, but you need to add an adapter data or control certain style to it and to use such controls is we say today AdapterView.

A: AdapterView-> V (View)

1: The Role

Display data in the form of a list

2: Content

AdapterView content generally contains a list of many of the same format resources

3: Features

(1) The front end and rear end of the display data separation

(2) the content can not be added in the form ListView.add list item, a need to specify Adapter object, which is obtained by the display data

(3) ListView MVC frame corresponds to V (view)

(4) Adapter frame corresponds MVC C (control)

(5) corresponds to a data source MVC frame M (models)

(6) After the outside of the screen, automatically add scroll bars

4: Classification

(1) ListView: list, which only contains a control TextView,

(2) Spinner: drop-down list to select users

(3) Gallery: thumbnail, has been replaced by the level of ScrollView and ViewPicker, but also fairly common and is a key center can handle lock, horizontal scrolling list

(4) GridView: trellis diagram showing resource in tabular form, may slide around

5: Event

(1) a user clicks a list item event

◆ load a list setOnItemClieckListener (click the Line ID in ListView event list object, click the control object view, location position in the list, click the list item) monitor, rewrite onItemClick method.

(2) User Press Event

◆ (click the Line ID in ListView event list object, click the control object view, location position in the list, click the list item) is loaded as a list setOnItemLongClieckListener monitor, rewrite onItemLongClick method.

6: Data filled

Adapter with the aid of the following object from the data source is adapted

Two: Adapter-> C (control)

1: The Role

The data in the data source is displayed in a view to a style (xml file).

2: Classification

(1) ArrayAdapter: list of items he can handle the full text of the case.

◆ Data source: Array or List object or other

(2) SimpleAdapter: he not only can handle the full text of the list item, when the list of items when there are other controls, can also be processed.

◆ Data Source: only for the List <Map < "key", "value" in the form of data >>

(3) Custom Adapter: filling awakened list items according to the pattern defined in the xml file, the applicability of the strongest.

(4) SimpleCursorAdapter: dedicated to the image data in the cursor to the list (we come back after study)

3: Custom Adapter

(1) create a class that inherits from BaseAdapter

(2) a method wherein the override four

①int getCount (): returns the number of source object data, i.e., the number of list items

②Object getItem (int position): Returns a list of the specified location position

③long getItemId (int position): Returns the row ID at the specified position

④View getView (): returns a list of items corresponding to the view of the body of the method

◆ materialized view filler

◆ with a view of the filler according to Xml document, materialized views

◆ find the control layout, and set properties

◆ return View View

Three: Data filled

1: Statement AdapterView objects, find the object ID according to the method using the findViewById

2: declared Adapter object, according to the method of Example of the configuration of the object. details as follows:

(1) ArrayAdapter <data type> adapter = new ArrayAdapter <data type> (context: Activity object generally refers to the current, layout: layout, data of each list of items displayed: a data source variable);

(2) SimpleAdapter adapter = new SimpleAdapter (context: Activity object generally refers to the current, data: data source variables, layout: each list item displayed layout, new String [] {}: the data source "key", new int [] {}: display control data source ID);

(3) Custom Adapter class adapter = new custom Adapter class constructor;

3: Binding to Adapter Object Adapter

AdapterView objects .setAdapter (Adapter Object);

 

 

Reference article: MartinDongChang

Guess you like

Origin www.cnblogs.com/doggod/p/12110484.html