Details about how to use Listview. How to Use listview
ListView can be called one of the most commonly used controls in Android. It is used by almost all applications. ListView allows you to scroll data out of the screen to the screen by swiping your fingers up or down. At the same time, the original data on the screen will scroll out of the screen.
Because ListView is used to display data, data cannot be directly transmitted to ListView, so we need to use an adapter to complete the process. The following describes the usage of ArrayAdapter, SimpleAdapter, and BaseAdapter for the two adapters.
ArrayAdapter
(1) Usage
It is used to bind an array and supports generic operations. Generally, you need to repeat the getView () method as needed.
(2) Advantages
Easy to use
(3) Disadvantages
Only simple text can be processed, or text with fixed Images
(4) constructor
ArrayAdapter (Context context, int textViewResourceId, List objects)
First parameter: current Context instance
Second parameter: The child layout file of the TextView control must be included.
SimpleAdapter
(1) Usage
Generally, the Activity that implements it can directly inherit the ListActivity, which is not much different from the Activity, but only optimizes the display ListView. The getView () method does not need to be rewritten.
(2) Advantages
It has the best scalability. You can define various la S. You can put ImageView, Button, and CheckBox;
(3) Disadvantages
The data source is complex. The data source of simpleAdapter is generally a List composed of HashMap. Each section of list corresponds to each item of ListView;
BaseAdapter
(1) Usage
It is the parent class of all adapters and is a general basic Adapter. When inheriting it, it must be rewritten in four ways: getCount (), getItem (), getItemId (), and getView.
(2) Advantages
Almost all functions can be implemented. Higher freedom. You can implement ListView with a Button control.
(3) Disadvantages
Because it is an abstract class, it requires four methods to be rewritten from time to inherit it, which is cumbersome.
(4) constructor
BaseAdapter (). You can write the constructor as needed.
(5) important knowledge points
In the use of ListView, you sometimes need to add buttons and other controls to implement separate operations. That is to say, the ListView does not only display data, but also processes user operations, but controls in it need to gain the user's focus. We can try to add a Button to the ListView entry using SimpleAdapter. We will find that the Button can be added but cannot get the focus. The click operation is overwritten by the ListView Item ,, unable to respond to the Button click event. At this time, the most convenient way is to use the flexible adapter BaseAdapter.
The following uses ArrayAdapter as an example to describe how to use listview;
First run the Code:
Activity_main.xml
Litem. xml
Student. java
package com.example.ryshine.listviewmodel;/** * Created by RyShine on 2018/1/22. */public class Student { private int id; private String name; private String address; private String time; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getTime() { return time; } public void setTime(String time) { this.time = time; }}
ArrayAdapter
StudentAdapter. java
Package com. example. ryshine. listviewmodel; import android. content. context; import android. support. annotation. nonNull; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. arrayAdapter; import android. widget. textView; import java. util. list;/*** Created by RyShine on 2018/1/22. */public class StudentAdapter extends ArrayAdapter
{Private int resourceId; public StudentAdapter (Context context, int resource, List
Objects) {// override the parent class constructor, parameters: context, listview subitem layout id, data super (context, resource, objects); resourceId = resource ;} @ NonNull @ Override public View getView (int position, View convertView, ViewGroup parent) {// Override the getView Method Student student = getItem (position); View view View; ViewHolder viewHolder; if (convertView = null) {// a. We can determine in the getView method. // if convertView is null, LayoutInFlater is used to load view = LayoutInflater. from (GetContext ()). inflate (resourceId, parent, false); viewHolder = new ViewHolder (); // 2. When converView is null, create a ViewHolder object, // store all the control instances in ViewHolder. id = (TextView) view. findViewById (R. id. id); // obtain the viewHolder of the control instance. name = (TextView) view. findViewById (R. id. name); viewHolder. address = (TextView) view. findViewById (R. id. address); viewHolder. time = (TextView) view. findViewById (R. id. time); view. se TTag (viewHolder); // 3. Call the setTag () method to obtain ViewHolder. // all the control instances are cached in ViewHolder, // you do not need to use findviewbyid () to obtain the control instance} else {// B. If it is not null, you can directly reuse the conveView to Improve the efficiency. view = convertView; viewHolder = (ViewHolder) view. getTag ();} viewHolder. id. setText (String. valueOf (student. getId (); // call the set Method of the control to set text and other information viewHolder. name. setText (student. getName (); viewHolder. address. setText (student. getAddress (); viewHold Er. time. setText (student. getTime (); return view; // return layout, so that a custom adapter completes} class ViewHolder {// 1. A new internal class ViewHolder is added, used to cache the instance of the control. TextView id; TextView name; TextView address; TextView time ;}}
Main Program
MainActivity. java
Package com. example. ryshine. listviewmodel; import android. support. v7.app. appCompatActivity; import android. OS. bundle; import android. util. log; import android. view. view; import android. widget. button; import android. widget. listView; import java. util. arrayList; import java. util. list; public class MainActivity extends AppCompatActivity {private List
StudentList = new ArrayList <> (); private Button insertBtn; private int id; private String name, address, time; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); ListView listView = (ListView) findViewById (R. id. listview); insertBtn = (Button) findViewById (R. id. insert); id = 0; name = "zhangsan"; address = "ganyu"; time = "2000"; final StudentAdapter studentAdapter = new StudentAdapter (MainActivity. this, R. layout. item, studentList); listView. setAdapter (studentAdapter); insertBtn. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {Student student = new Student (); id ++; // id auto-increment 1 student. setId (id); Log. I ("flag", String. valueOf (id); student. setName (name); Log. I ("name", name); student. setAddress (address); Log. I ("address", address); student. setTime (time); Log. I ("time", time); studentList. add (0, student); // the latest data is displayed in the first row of studentAdapter. notifyDataSetChanged (); // notification data update }});}}