当前位置:网站首页>Use the list component to realize the drop-down list and address list
Use the list component to realize the drop-down list and address list
2022-07-06 02:05:00 【Standing on the shoulders of giants, review the past and know t】
The list component contains a drop-down list box (Spinner) And list view (ListView).

chart 1 List component inheritance diagram
As can be seen from the above figure ,Spinner and ListView Inherit from ViewGroup, So they all belong to View Containers ; Inherit from AdapterView, So you can display multiple list items .
1 Drop down list box
<Spinner> The basic syntax is as follows :
<Spinner
android:entries="@array/ Array name "
android:prompt="@string/info"
Other attributes
>
</Spinner>android:entries For optional properties , Used to specify list items ;android:prompt For optional properties , Used to specify the title of the drop-down list box .
Spinner: Implementation drop down list
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create a listening event for the drop-down list
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String result = parent.getItemAtPosition(position).toString();
Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}https://github.com/hanyuhang-hz/android-demos
2 List view
<ListView> The basic syntax is as follows :
<ListView
Property list
>
</ListView>ListView Commonly supported xml The attributes are shown in the table 2:
xml attribute | describe |
android:divider | Set the separator bar for the list view |
android:dividerHeight | Set the height of the separator |
android:entries | Through the array, the resource is ListView Specify the list item |
android:footerDividersEnabled | Is the setting footer View( Bottom view ) Draw the divider bar before |
android:headerDividersEnabled | Is the setting header View( Head view ) Then draw the separator |
surface 2 ListView Commonly supported xml attribute
ListView: Realize the address book list
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listview = (ListView) findViewById(R.id.listview);
int[] imageId = new int[]{R.mipmap.img01, R.mipmap.img02, R.mipmap.img03,
R.mipmap.img04, R.mipmap.img05, R.mipmap.img06,
R.mipmap.img07, R.mipmap.img08, R.mipmap.img09,
};
String[] title = new String[]{" that ", " Chen er ", " Zhang San ", " Li Si ", " Wang Wu ",
" Zhao Liu ", " Sun Qi ", " Tuesday ", " Wu Jiu "};
List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
for (int i = 0; i < imageId.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", imageId[i]);
map.put(" name ", title[i]);
listItems.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this, listItems,
R.layout.main, new String[] { " name ", "image" }, new int[] {
R.id.title, R.id.image });
// Connect the adapter to ListView relation
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Map<String, Object> map = ( Map<String, Object> )parent.getItemAtPosition(position);
Toast.makeText(MainActivity.this,map.get(" name ").toString(),Toast.LENGTH_SHORT).show();
}
});
}
}边栏推荐
- Cookie concept, basic use, principle, details and Chinese transmission
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- dried food! Accelerating sparse neural network through hardware and software co design
- 【Flask】官方教程(Tutorial)-part2:蓝图-视图、模板、静态文件
- Sword finger offer 12 Path in matrix
- [solution] add multiple directories in different parts of the same word document
- 【clickhouse】ClickHouse Practice in EOI
- leetcode3、实现 strStr()
- D22:indeterminate equation (indefinite equation, translation + problem solution)
- Selenium waiting mode
猜你喜欢

Computer graduation design PHP enterprise staff training management system

Redis string type

leetcode3、實現 strStr()

Using SA token to solve websocket handshake authentication
![[flask] official tutorial -part3: blog blueprint, project installability](/img/fd/fc922b41316338943067469db958e2.png)
[flask] official tutorial -part3: blog blueprint, project installability

Publish your own toolkit notes using NPM
Folio. Ink is a free, fast and easy-to-use image sharing tool
![抓包整理外篇——————状态栏[ 四]](/img/1e/2d44f36339ac796618cd571aca5556.png)
抓包整理外篇——————状态栏[ 四]
![[depth first search] Ji Suan Ke: Betsy's trip](/img/b5/f24eb28cf5fa4dcfe9af14e7187a88.jpg)
[depth first search] Ji Suan Ke: Betsy's trip

Cookie concept, basic use, principle, details and Chinese transmission
随机推荐
竞赛题 2022-6-26
Redis-列表
[solution] every time idea starts, it will build project
抓包整理外篇——————状态栏[ 四]
01. Go language introduction
Paddle框架:PaddleNLP概述【飞桨自然语言处理开发库】
Redis key operation
Visualstudio2019 compilation configuration lastools-v2.0.0 under win10 system
module ‘tensorflow. contrib. data‘ has no attribute ‘dataset
【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库
[solution] add multiple directories in different parts of the same word document
GBase 8c数据库升级报错
Basic operations of databases and tables ----- unique constraints
Computer graduation design PHP part-time recruitment management system for College Students
Publish your own toolkit notes using NPM
You are using pip version 21.1.1; however, version 22.0.3 is available. You should consider upgradin
Numpy array index slice
Poj2315 football games
Accelerating spark data access with alluxio in kubernetes
genius-storage使用文档,一个浏览器缓存工具