ArrayAdapter的功能比較有限脆霎,他的列表項只能是TextView袁梗,如果開發(fā)實現(xiàn)需要更加復(fù)雜的列表項细燎,則可以考慮SimpleAdapter
首先定義一個ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="horizontal">
<ListView
android:id="@+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
該listView將顯示simpleAdapter提供的列表項
下面是Activity代碼:
package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private String[] names=new String[]{"虎頭","弄玉","李清照","李白"};
private String[] descs=new String[]{"可愛的小孩","一個擅長音樂的女孩","一個擅長文學(xué)的女性","一個小魯班的巴巴"};
private int [] imageIds=new int[]{R.drawable.tx,R.drawable.tx,R.drawable.tx,R.drawable.tx};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//創(chuàng)建一個list集合适滓,list集合的元素是map
List<Map<String,Object>> listItems=new ArrayList<>();
for (int i=0;i<names.length;i++)
{
Map<String,Object> listItem=new HashMap<>();
listItem.put("header",imageIds[i]);
listItem.put("personName",names[i]);
listItem.put("desc",descs[i]);
listItems.add(listItem);
}
//創(chuàng)建一個SimpleAdapter
SimpleAdapter simpleAdapter =new SimpleAdapter(this,listItems,R.layout.simple_item,new String[]{"personName","header","desc"},new int[]{R.id.name,R.id.header,R.id.desc});
ListView list=findViewById(R.id.mylist);
//為ListView設(shè)置Adapter
list.setAdapter(simpleAdapter);
}
}
simpleAdapter需要個參數(shù)
第一個this
第二個參數(shù):該參數(shù)應(yīng)該是一個List<?extends Map<String,?>>類型的集合對象,該集合中每個Map<String,?>對象生成一個列表項钠怯。
第三個參數(shù):該參數(shù)是個布局id佳魔。該。xml文件作為列表項組件晦炊。
第四個參數(shù):該參數(shù)是個String【】類型的參數(shù)鞠鲜,該參數(shù)取決定提取Map<String,?>對象中那些key對應(yīng)的value來生成列表項
第五個參數(shù):該參數(shù)應(yīng)該是一個int【】類型的參數(shù)宁脊,該參數(shù)決定補(bǔ)充那些組件
從上面的程序看。listItems是一個長度為4的集合贤姆,這就意味著它生成的ListView將會包含4個列表項榆苞,每個列表項都是R.layout.simple_item對應(yīng)的組件(也就是一個LinearLayout組件)。LinearLayout中包含了3個組件霞捡,這些組件內(nèi)容由listItems集合提供坐漏。
R.layout.simple.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="horizontal">
//定義一個ImageView,用于作為列表項的一部分
<ImageView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textColor="#f0f"
android:textSize="20dp"/>
<TextView
android:id="@+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textSize="14dp"/>
</LinearLayout>
</LinearLayout>
效果圖:
知識拓展:
設(shè)置監(jiān)聽用戶點擊碧信,通過AdapterView的setOnItemClickListener()方法
在activity中加入如下代碼監(jiān)聽
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("111",names[position]+"被選中了");
}
這里小編遇到一個報錯就是log提示不存在
其實Log已經(jīng)報紅了赊琳,蠻嘗試下運行(快捷鍵shift+F10)下,就會看到報錯信息音婶,其實很簡單的問題,就是忘記導(dǎo)包了慨畸!
添加:
import android.util.Log;