Learning process of ListView control for Android

Learning and error correction process of ListView control for Android

School experiment implementation: establish a ListView component in the program interface. The component is divided into four lines with three items in each line. It displays "Zhao Linnan 20; Qian jienan 24; sun hongnv 22; Li Chengnan 29" respectively. Click a line of the ListView component to display "you selected XX" in the Toast component. Such a program. Also encountered many problems.

Introduction: ListView is equivalent to a two-dimensional array, which can be realized by creating a one-dimensional array and adding it to ListView. Each row of the ListView represents each object to be filled in the title.

File creation aspect

At first, I thought that there were several rows in the corresponding ListView, so I had to create xml files for several rows. Later, I learned that I only needed to create an xml file representing the row format, as shown in the following figure:

Control add aspect
You only need to add ListView control to main.xml_ Add the format of each line of the ListView to be created in the item.xml file. In this example, the three textview controls are directly dragged.

Programming aspects
At first, I didn't understand simpleadapter msimpleader = new simpleadapter (this, listitem, r.layout.list_item, new string [] {"name", "sex", "age"}, new int[]{R.id.textView1,R.id.textView2,R.id.textView3});
Written as:

  SimpleAdapter mSimpleAdapter=new SimpleAdapter(this,listItem,R.layout.main,new String[]{"textView_1","textView_2","textView_3"},new int[]{R.id.textView1,R.id.textView2,R.id.textView3,R.id.textView3});
    lv.setAdapter(mSimpleAdapter);  

As a result, when the ListView interface is displayed later, there is nothing
In fact, the third parameter here should represent the interface layout of receiving listitem (the second parameter), that is, the format of each line required by the topic, that is, the created list_item.xml file. Instead of the main.xml file you thought.

ps:
The first parameter, this, refers to the context
The second parameter, listItem (name defined): list based on map. Each item in the listItem two-dimensional array corresponds to each item in the ListView. Each item in listItem is a map type. This map class contains the data required for each row of ListView. In fact, it is a two-dimensional array used to store all data, which is just composed of map type variables.
The fourth parameter: String array. Each item in the array should correspond to the key value stored in the map set in the second parameter.
The fifth parameter: int array. Each item in the array should correspond to the values stored in the map set in the second parameter. It's actually a list_ The corresponding ID in the item.xml file.

After the change is as follows:

		HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("name", "Zhao Lin");
        map.put("sex", "male");
        map.put("age", "20");
        listItem.add(map);
        HashMap<String,Object> map1=new HashMap<String,Object>();
        map1.put("name", "Qian Jie");
        map1.put("sex", "male");
        map1.put("age", "24");
        listItem.add(map1);
        HashMap<String,Object> map2=new HashMap<String,Object>();
        map2.put("name", "Sun Hong");
        map2.put("sex", "female");
        map2.put("age", "22");
        listItem.add(map2);
        HashMap<String,Object> map3=new HashMap<String,Object>();
        map3.put("name", "Li Cheng");
        map3.put("sex", "male");
        map3.put("age", "29");
        listItem.add(map3);
        //This is R.layout.list_item is not main
        SimpleAdapter mSimpleAdapter=new SimpleAdapter(this,listItem,R.layout.list_item,new String[]{"name","sex","age"},new int[]{R.id.textView1,R.id.textView2,R.id.textView3});
        lv.setAdapter(mSimpleAdapter);

Finally realize

The junior contestant said that it took a lot of time to write the code this time. For the called function, you must be optimistic about the parameters before acting. What you think cannot become what you think for the computer. I hope the bug s in the future will be tolerant to me ~ ~.

Keywords: Java Android UI

Added by iceblox on Sat, 04 Dec 2021 07:26:58 +0200