接着上篇文章:
二. 通过构建Adapter(适配器)来在ListView显示一个组合控件
UI的xml代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/myList"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</LinearLayout>
list.xml是在ListView中的每个Item的布局xml文件,代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<!-- 定义一个ImageView作为列表项的一部分 -->
<ImageView
android:id="@+id/header"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingLeft="10dp"
/>
<TextView
android:id="@+id/name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="16dp"
android:gravity="center_vertical"
android:paddingLeft="10dp"
/>
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入"
android:layout_gravity="center"
/>
</LinearLayout>
Activity的代码如下:
package com.example.simpleadaptertest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.support.v4.app.NavUtils;
public class SimpleAdapterTest extends Activity {
private String [] names=new String[] {"虎头","弄玉","李清照","李白"};
private int [] imageIds=new int[]{R.drawable.tiger, R.drawable.nongyu,R.drawable.qingzhao,R.drawable.libai};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_adapter_test);
//创建一个List集合,List集合的元素是Map
List<Map<String, Object>> listItems=new ArrayList<Map<String,Object>>();
for(int i=0;i<names.length;i++){
Map<String, Object> listItem=new HashMap<String, Object>();
listItem.put("header", imageIds[i]);
listItem.put("personName", names[i]);
listItems.add(listItem);
}
SimpleAdapter simpleAdapter=new SimpleAdapter(this, listItems, R.layout.list, new String[]{"personName","header"}, new int[]{R.id.name,R.id.header});
ListView list=(ListView)findViewById(R.id.myList);
list.setAdapter(simpleAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_simple_adapter_test, menu);
return true;
}
}
先看运行结果:
在Activity代码中的第34行:SimpleAdapter simpleAdapter=new SimpleAdapter(this, listItems, R.layout.list, new String[]{"personName","header"}, new int[]{R.id.name,R.id.header});中的R.layout.list即显示每个Item中要显示的组合控件以及其布局。