ListView四种适配器:
1、ArrayAdapte
2、SimpleAdapter
3、SimpleCursorAdapter
4、BaseAdapter
下面是基于后面3中适配器实现的,还有一个就是SimpleAdapter的加强版,也就是plus,哈哈。主要是实现带图片功能,第一种适配器比较简单,准备用它实现显示分组。下面是实现功能截图以及代码。
上图实现的核心代码:
public class ListView1 extends Activity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview1);
listView = (ListView) this.findViewById(R.id.listView1);
showSimpleAdapter();
}
private void showSimpleAdapter() {
List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
for (int i = 0; i < 4; i++) {
HashMap<String, Object> item = new HashMap<String, Object>();
item.put("name","atm"+i );
item.put("age",10+i );
data.add(item);
}
/**
* Context context 上下文信息
List<? extends Map<String, ?>> data 将该数据显示在界面上
int resource 数据需要显示的地方
String[] from 从什么地方
int[] to 显示到什么地方
*/
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,new String[]{"name", "age"}, new int[]{R.id.text1,R.id.text2});
listView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
上图实现的核心代码:
public class ListView2 extends Activity {
private ListView listView;
private ServiceTest serviceTest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview1);
listView = (ListView) this.findViewById(R.id.listView1);
serviceTest=new ServiceTest(getApplicationContext());
showSimpleCursorAdapter();
}
private void showSimpleCursorAdapter() {
Cursor cursor =serviceTest.Find_cursor();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor,
new String[]{"name", "age"}, new int[]{R.id.text1, R.id.text2});
listView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
上图实现的核心代码:
public class ListView3 extends Activity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview2);
listView = (ListView) this.findViewById(R.id.listView2);
showSimpleAdapter();
}
private void showSimpleAdapter() {
SimpleAdapter adapter = new SimpleAdapter(
this, getData(), R.layout.item_plus,new String[]{"name", "age", "img"}, new int[]{R.id.name,R.id.age,R.id.img});
listView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "atm1");
map.put("age", 1);
map.put("img", R.drawable.img2);
list.add(map);
map = new HashMap<String, Object>();
map.put("name", "atm2");
map.put("age", 2);
map.put("img", R.drawable.love);
list.add(map);
map = new HashMap<String, Object>();
map.put("name", "atm3");
map.put("age", 3);
map.put("img", R.drawable.img1);
list.add(map);
return list;
}
}
上图实现的核心代码:
public class ListView4 extends Activity {
private ListView listView;
private ServiceTest serviceTest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview1);
listView = (ListView) this.findViewById(R.id.listView1);
serviceTest=new ServiceTest(this);
listView.setOnItemClickListener(new ItemClickListener());
showMyAdapter();
}
//点击后,获取给View上的数据
private final class ItemClickListener implements OnItemClickListener{
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListView listview = (ListView)parent;
UserInfo userinfo = (UserInfo) listview.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), userinfo.getUser_id()+" "+userinfo.getUser_name(), 1).show();
}
}
private void showMyAdapter() {
// TODO Auto-generated method stub
List<UserInfo> list=serviceTest.Findall();
MyAdapter myAdapter=new MyAdapter(this, list, R.layout.item);
listView.setAdapter(myAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
代码下载请点击:代码下载
补充说明:在点击SimpleAdapter前,先通过源代码中的单元测试中的方法,添加数据。