惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News | PayPal Newsroom
云风的 BLOG
云风的 BLOG
GbyAI
GbyAI
Engineering at Meta
Engineering at Meta
B
Blog RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Register - Security
The Register - Security
L
LangChain Blog
A
About on SuperTechFans
S
Schneier on Security
博客园 - 三生石上(FineUI控件)
Stack Overflow Blog
Stack Overflow Blog
The Hacker News
The Hacker News
AWS News Blog
AWS News Blog
博客园 - 司徒正美
Scott Helme
Scott Helme
K
Kaspersky official blog
Cyberwarzone
Cyberwarzone
T
Tenable Blog
腾讯CDC
Recorded Future
Recorded Future
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
G
GRAHAM CLULEY
Security Latest
Security Latest
S
Securelist
D
Darknet – Hacking Tools, Hacker News & Cyber Security
aimingoo的专栏
aimingoo的专栏
Google DeepMind News
Google DeepMind News
V
Vulnerabilities – Threatpost
雷峰网
雷峰网
T
The Exploit Database - CXSecurity.com
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
V2EX
T
The Blog of Author Tim Ferriss
D
Docker
S
Security Affairs
F
Full Disclosure
Know Your Adversary
Know Your Adversary
N
News and Events Feed by Topic
N
News and Events Feed by Topic
T
Tor Project blog
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Microsoft Security Blog
Microsoft Security Blog
Simon Willison's Weblog
Simon Willison's Weblog
Recent Announcements
Recent Announcements
博客园_首页
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Security @ Cisco Blogs

CrazyWong

CrazyWong AI LOGO 🎧 Anson Seabra - Keep Your Head Up Princess 是的,我又更換了域名 1.5 萬字 CSS 基礎拾遺(核心知識、常見需求) JavaScript - 日期 Date JavaScript - 數組 Array 域名更換 前端 JavaScript 自測清單 2 前端 JavaScript 自測清單 1 通過travis-ci或者GitHub Actions自動化部署GitHub Pages和Coding Pages 添加Windows Terminal到鼠標右鍵菜單 調研實現高性能動畫 在瀏覽器輸入 URL 回車之後發生了什麼(超詳細版) hexo-theme-butterfly 安裝文檔(一)快速開始 設置Windows電腦自動關機 Windows必裝軟件推薦 關於字符編碼,你所需要知道的(ASCII,Unicode,Utf-8,GB2312…) 好用的新浪圖床工具推薦 - Weibo-Picture-Store Java知識點複習(二) Java知識點複習(一) Visual Studio Code 插件推薦-VSC Netease Music 重裝系統後重新部署恢復 Hexo blog Windows上Java的環境變量配置 9Patch 介紹 第一行代碼筆記-RecyclerView 為Blog添加版權説明 第一行代碼筆記-ListView 第一行代碼筆記-創建自定義控件 第一行代碼筆記-四大Layout Hexo和Next主題的相關設置(持續更新) 第一行代碼筆記-bulid.gradle 解析 第一行代碼筆記-Android Studio工程目錄結構介紹 第一行代碼筆記-工具日志log
Adapter
MYW · 2018-07-20 · via CrazyWong

Adapter 只是一個接口,其派生了ListAdapter和SpinnerAdater兩個子接口。
ListAdater為AbsListView提供列表項,SpinnerAdater為AbsSpinner提供列表項。
以下是Adapter相關類的關係圖

Adapter常用的實現類:

  • ArrayAdapter: 通常用於將數組或者List集合的多個值包裝成多個列表項。
  • SimpleAdapter: 用於將List集合的多個對象包裝成多個列表項。
  • SimpleCursorAdapter: 與SimpleAdapter基本相似,只是用於包裝Cursor提供的數據。
  • BaseAdapter: 通常用於被擴展。擴展BaseAdapter可以對各列表項進行最大限度地定製。

使用ArrayAdapter創建ListView

修改activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list1"
android:divider="#f00"
android:dividerHeight="2px"
android:headerDividersEnabled="false"></ListView>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list2"
android:divider="#0f0"
android:dividerHeight="2px"
android:headerDividersEnabled="false"></ListView>
</LinearLayout>

android:divider: 可以實現分割線,可以用圖片或者顏色
android:dividerHeight 設置分割線的高度
android:headerDividersEnabled 是否顯示頭部的分割線,默認是true

修改MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list1 = (ListView) findViewById(R.id.list1);
String [] arr1 = {"孫悟空","豬八戒","牛魔王"};
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,R.layout.array_item,arr1);

list1.setAdapter(adapter1);
ListView list2 = (ListView) findViewById(R.id.list2);
String[] arr2 = { "java","hibernate","spring","android"};
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,R.layout.checked_item,arr2);
list2.setAdapter(adapter2);
}
}

ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,R.layout.array_item,arr1);

ArrayAdapter傳入了三個參數:

  • Context: 代表了訪問整個Android應用的接口。

  • textViewResourceId: 一個TextView的資源ID,該TextView組件將作為ArrayAdapter的列表項組件。

  • 數組或者List: 提供數據。

以上代碼可以看到,該數組或List包含多少個元素,就將會生成多少個列表項,每個列表項都是TextView組件。
arr1有三個數據,則會生成一個包含三個列表項的ArrayAdapter,每個列表項的組件外觀由R.layout.array_item佈局文件(該佈局文件只是一個TextView)控制。

新建array_item.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/TextView"
android:textSize="24dp"
android:padding="10px"
android:shadowColor="#f0f"
android:shadowDx="4"
android:shadowDy="4"
android:shadowRadius="2"/>

TextView-shadow 陰影實現方式

  • android:shadowColor:陰影的顏色

  • android:shadowDx:水平方向上的偏移量

  • android:shadowDy:垂直方向上的偏移量

  • android:shadowRadius:是陰影的的半徑大小,值也大,陰影越大

新建 checked_item.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>

<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/checktextview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
android:padding="10px"
android:checked="true"
android:checkMark="?android:listChoiceIndicatorMultiple"
/>

運行結果:

基於ListActivity實現列表

如果程序僅僅只是顯示一個列表,那麼可以無需通過設置xml來實現,而是直接讓Activity繼承ListActivity來實現。

修改MainActivity.java

1
2
3
4
5
6
7
8
9
10
11

public class Main2Activity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
String[] atr = {"孫悟空","豬八戒","牛魔王"};
ArrayAdapter <String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,atr);
setListAdapter(adapter);

}
}

可以看到,不用使用setContentView()去調用佈局文件。

運行結果:

版權聲明: 本部落格所有文章除特別聲明外,均採用CC BY-NC-SA 4.0 授權協議。轉載請註明來源 CrazyWong