Android development advanced components--ListView (list display component)

1. Similar to ScrollView, there is also a list component ListView, which can add multiple components to ListView to achieve the scrolling display effect of components.

2. The hierarchical relationship of ListView is as follows:
   java.lang.Object
      android.view.View
         android.view.ViewGroup
            android.widget.AdapterView<T extends android.widget.Adapter>
               android.widget.AbsListView
                  android.widget.ListView

3. ListView is a frequently used component, and each sub-item Item in it can be a string or a combination component. To implement the ListView component, there are the following steps:
   1) Prepare the data to be displayed by the ListView.
   2) Build an adapter, which is an Item array, and generate as many Items as there are elements in the dynamic array. There are three common types of adapters: ArrayAdapter, SimpleAdapter and SimpleCursorAdapter. ArrayAdapter is the simplest and can only display one line of text. SimpleAdapter has the best scalability and can customize various effects. SimpleCursorAdapter is a simple combination of SimpleAdapter to the database, which can easily display database records in the form of a list. SimpleAdapter inherits from AdapterView. You can add listeners to ListView through some methods. When the user clicks a list item, the corresponding operation is performed.
   3)把适配器添加到ListView并显示出来。

4、新建布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/LinearLayout">

    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

5、新建ListViewActivity.java
package xiao.fuyan.testapp;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
* Created by xiao on 2017/1/1.
*/
public class ListViewActivity extends Activity {

    private String listData[] = {"Information School", "Mechanical School", "Computer School" , "School of Journalism", "School of Chemical Engineering",
            "School of Fine Arts", "School of Computer", "School of Journalism", "School of Chemical Engineering", "School of Fine Arts", "School of Physical Education", "School of Music",
            "School of Economics and Management" ", "South Lake College", "Physics and Electronics", "Mechatronics", "Law", "Foreign Languages",
            "Technology Office", "Library", "Department of Academic Affairs", "Network Center", " Academic Department", "Finance Department", "Information College", "Mechanical College", "Computer College", "Journalism College", "Chemical College",
            "Art College", "School of Computer Science", "School of Journalism", "School of Chemical Engineering", "School of Fine Arts", "School of Physical Education", "School of Music",
            "School of Economics and Management", "South Lake College", "School of Physics and Electronics", "School of Mechatronics" ", "School of Law", "School of Foreign Languages",
            "Technology Office", "Library", "Department of Academic Affairs", "Network Center", "School of Education", "Finance Office"};
    private ListView listView;

    @Override    @Override     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_xml);

        listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_expandable_list_item_1,listData));
    }
}


Guess you like

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