Android: Preliminary study of Activity, Adapter, List

Activity

An Activity is an application component that the user can provide to interact with the screen to perform actions on the phone. Each Activity gets a window on which to draw its user interface. The window will generally fill the screen, but not necessarily, it can float on top of other windows.

In simple terms, an application usually consists of multiple activities that are loosely related to each other. Typically, an Activity in an application is designated as the "main" Activity, which is the Activity that is presented to the user when the application is first launched. And each Activity can start another Activity to perform a different action. Every time a new activity starts, the previous activity is stopped, but the system keeps the activity on the stack ("back stack"). When a new activity starts, the system pushes it onto the back stack and takes user focus. The back stack follows a basic "last in first out" stack mechanism, so when the user finishes the current activity and presses the "back" button, the system pops it from the stack (and destroys it) and resumes the previous activity.

 

Related functions about Activity

onCreate(): Start to create an Activity, the first method executed by the system.


onStart(): called just before the Activity becomes visible


onRestart(): Called when the Activity returns to the foreground from the background, visible to the user but not interactive


onResume(): Called just before the Activity starts interacting with the user. (Not fully interactive at this time)


onWindowFocusChanged(): Called when the Activity window gains or loses focus, after onResume or after onPause


onPause(): Called when the Activity is covered below or the screen is locked


onStop(): Called when exiting the current Activity or jumping to a new Activity


onDestroy(): Called when the current Activity is exited, and the Activity ends after the call


onSaveInstanceState(Bundle outState): Called when the Activity is killed by the system. For example: when the screen orientation changes, the Activity is destroyed and rebuilt; the current Activity is in the background, and system resources are tight to kill it. In addition, when jumping to other Activity or This method will also be called when you press the Home button to return to the home screen. The system is to save the state of the current View component.
It is called before onPause.


onRestoreInstanceState(Bundle savedInstanceState): Called when the Activity is killed by the system and then rebuilt. For example: when the screen orientation changes, the Activity is destroyed and rebuilt; the current Activity is in the background, and the system resources are tight to kill it, and the user starts the Activity again. In both cases onRestoreInstanceState will be called, after onStart.

 

After understanding the above related functions, look at the cycle diagram of this Activity to better understand the execution process of the Activity

 

                                

 

Adapter

 

Adapter is an adapter interface that connects back-end data and front-end display, and is an important link between data and View.

 

There are many types of Adapter in Android, the more commonly used are BaseAdapter, SimpleAdapter, ArrayAdapter, SimpleCursorAdapter and so on.

 

BaseAdapter: The basic adapter. Android BaseAdapter implements the interfaces of ListAdapter and SpinnerAdapter, and the GridView adapter implements ListAdapter. Therefore, BaseAdapter is common to Spinner, ListView, and GridView. BaseAdapter is an abstract class, and the class that inherits it needs to implement many methods.

 

ArrayAdapter: ArrayAdapter is like BaseAdapter, derived from BaseAdapter, it has all the functions of BaseAdapter. However, ArrayAdapter can use generic structs directly.

 

ArrayAdapter is relatively simple and can only display one line of text.

 

SimpleAdapter: A simple adapter that can map static data to a layout defined by an XML file. Using SimpleAdapter can display more complex lists, for example, each line displays some complex/special effects such as some pictures, texts, etc., but it is only a simple display, if the list is modified later, it is not possible. SimpleAdapter has the final extension.

 

SimpleCursorAdapter: SimpleCursorAdapter is a UI component specially used to display data in database tables. In Android, some ordinary Adapter objects can also display the data in the database on the Azi interface, but the workload of using ordinary Adapter is much larger. SimpleCursorAdapter is very similar in use. SimpleAdapter can be understood as a simple combination of databases, and then the contents of the database are displayed in the form of lists.

 

 

Adapter is the parent class of all Adapter subclasses. It is an interface that defines many basic methods. As a bridge connecting AdapterView and dataset. It can get each item in the data collection and match it with the corresponding view item of AdapterView one by one to display the data.

The main method of Adapter:

int getCount():

Gets the total number of items in the data collection connected to the current Adapter.

 

Object getItem(int Posistion):

Get the item with the specified subscript in the dataset bound to the current Adapter.

 

long getItemId(int position):

Get the row id of the item with the specified subscript in the dataset bound to the current Adapter (that is, the subscript in the list)

 

 

View getView(int position,View convertView,ViewGroup parent):

This method is the most difficult and most important place to implement Adapter subclasses, and it is often the place where problems are most likely to occur, so special attention should be paid to it. The purpose of this method is to get a view that displays the data item at the specified index in the current dataset. There are generally two ways to get a View, create a View yourself or fill it with an XML layout to get a View. Under normal circumstances, when filling a View, if the root view of the View is not specified in the LayoutInflater.inflate method, the current View will be attached to the parent view (ListView....) by default, and the default layout parameters will be used. fill it.

 

int getItemViewType(int position):

The result returned by it can be used to determine the view type used to populate the data item with the specified subscript in the dataset. The value returned by this method should be between 0 - (result returned by getViewTypeCount - 1).

 

int getViewTypeCount():

Returns the sum of the types of views that getView may return. If getView returns the same result every time, this method returns 1. The adpterView view has several different views, and this method returns as many.

 

List

List: An ordered, repeatable collection of elements. Each element in the collection has its corresponding sequential index. The List collection allows repeating elements, and the collection element at a specified position can be accessed by index.
We can add elements directly through the add method. When using the add method, we can also insert data into a specific position according to the index value; we can modify the element at the specified index value position through the set method; we can obtain the index value of the specified element.
 

List is an interface, ArrayList, LinkList inherit from this interface and implement it.

ArrayList

Instantiate:

ArrayList<Object> arrayList = new ArrayList<Object>();

Common method:

The root class of ArrayList is collection, so ArrayList can use the methods defined in collection

Such as:

add(); Add objects such as: arrayList.add("a");

get(); Get the subscript object in the arrayList such as: arrayList.get(1);

size(); Get the number of elements in the arrayList such as: arrayList.size();

remove(); delete a subscript object in arrayList such as: arrayList.remove(1);

 

LinkedList

 

LinkedList is a double-linked list, which has better performance than ArrayList when adding and removing elements. But it is weaker than ArrayList in terms of get and set. Of course, these comparisons refer to the comparison when the amount of data is large or the operation is very frequent . It also implements the Queue interface, which provides more methods than List, including offer(), peek(), poll(), etc.

 

 

ArrayList and LinkedList have their own advantages and disadvantages in performance, and each has its own application. Generally speaking, it can be described as follows: 


1. Inserting or removing an element in the middle of an ArrayList means that the remaining elements in the list will be moved; while inserting or removing an element in the middle of a LinkedList has a fixed cost. 

2. LinkedList does not support efficient random element access. 

3. The space waste of ArrayList is mainly reflected in the reservation of a certain capacity space at the end of the list list, while the space cost of LinkedList is reflected in that each element needs to consume a considerable amount of space. 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325170664&siteId=291194637