SimpleAdapter是扩展性最好的适配器,可以定义各种你想要的布局,而且使用很方便
SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
参数context:上下文,比如this。关联SimpleAdapter运行的视图上下文
参数data:Map列表,列表要显示的数据,这部分需要自己实现,如例子中的getData(),类型要与上面的一致,每条项目要与from中指定条目一致
参数resource:ListView单项布局文件,这个布局就是你自定义的布局了,你想显示什么样子的布局都在这个布局中。这个布局中必须包括了to中定义的控件id
参数 from:一个被添加到Map上关联每一个项目列名称的列表,数组里面是列名称
参数 to:是一个int数组,数组里面的id是自定义布局中各个控件的id,需要与上面的from对应
data:这部分大概可以这样写:
private List<HashMap<String, Object>> Getdata() {
// TODO Auto-generated method stub
List<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();
for(int i=1;i<=10;i++){
HashMap<String,Object> map = new HashMap<String,Object>();
map.put("image", R.drawable.ic_launcher);
map.put("name", "name"+i);
map.put("age", "age"+i);
list.add(map);
}
return list;
}
resource :自己定义的布局xml文件,列表单项的内容;
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#00ff00"
>
<ImageButton
android:src="@drawable/back"//返回按钮
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="比赛报名"
android:textSize="20sp"
android:layout_centerInParent="true"
/>
</RelativeLayout>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
String[] from:
new String[]{"image","name","age"},
String[] to
new int[]{R.id.image,R.id.name,R.id.age});
Activity.java中的主程序:
package com.example.uilayout;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class ListActivity extends Activity {
private ImageButton back=null;
private ListView listview= null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
back=(ImageButton)findViewById(R.id.back);
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(ListActivity.this,LoginActivity.class);
startActivity(intent);
}
});
listview = (ListView) findViewById(R.id.listview);
SimpleAdapter adapter = new SimpleAdapter(
this,
Getdata(),
R.layout.user,
new String[]{"image","name","age"},
new int[]{R.id.image,R.id.name,R.id.age});
listview.setAdapter(adapter);
}
private List<HashMap<String, Object>> Getdata() {
// TODO Auto-generated method stub
List<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();
for(int i=1;i<=10;i++){
HashMap<String,Object> map = new HashMap<String,Object>();
map.put("image", R.drawable.ic_launcher);
map.put("name", "name"+i);
map.put("age", "age"+i);
list.add(map);
}
return list;
}
}