列表三元素:
1、展示列表的View(ListView)
2、适配器(即View与数据的中介 - adapter)
3、数据(data)
public class MainActivity extends ListActivity {
private List<Map<String, Object>> data;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
data = getData();
MyAdapter adapter = new MyAdapter(this);
setListAdapter(adapter);
}
public List<Map<String, Object>> getData() {
Map<String, Object> map = new HashMap<String, Object>();
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
map.put("name", "zz");
map.put("age", "21");
map.put("num", "20103436");
data.add(map);
map = new HashMap<String, Object>();
map.put("name", "xy");
map.put("age", "21");
map.put("num", "20103426");
data.add(map);
map = new HashMap<String, Object>();
map.put("name", "ll");
map.put("age", "21");
map.put("num", "20103424");
data.add(map);
return data;
}
public final class ViewHolder {
public TextView name;
public TextView age;
public TextView num;
}
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public MyAdapter(Context context) {
this.mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
if(convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.vlist, null);
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.age = (TextView) convertView.findViewById(R.id.age);
holder.num = (TextView) convertView.findViewById(R.id.num);
convertView.setTag(holder);
}
else {
holder = (ViewHolder)convertView.getTag();
}
holder.name.setText((String)data.get(position).get("name"));
holder.age.setText((String)data.get(position).get("age"));
holder.num.setText((String)data.get(position).get("num"));
convertView.setOnLongClickListener(new longClick(position));
return convertView;
}
}
class longClick implements OnLongClickListener {
private int id;
public longClick(int id) {
this.id = id;
}
@Override
public boolean onLongClick(View v) {
Log.v("longClick", "success!" + id);
Toast.makeText(MainActivity.this, "序号为" + id + "长按事件成功", Toast.LENGTH_SHORT).show();
return false;
}
}
}
注(我出错的地方):
1、继承的是ListActivity,用setListAdapter()来对适配器就是配置;
2、data数据应该单独提出来作全局变量,因为后面会对数据进行操作(如提取大小、get()等...);
3、MyAdapter(本例中用的是BaseAdapter),其内部重写方法均是自动调用,如getView()会在setListAdapter(adapter)中自动调用;
4、ViewHolder是自定义的,需在getView()中对其赋值,如:holder.name.setText((String)data.get(position).get("name"));
5、本次长按事件是在getView()中实现;(convertView.setOnLongClickListener(new longClick(position));)
总结:
1、数据的实现getData(),用的是List<Map<String, Object>>,然后实例化HashMap<String, Objcet>,用map.add()来匹配值与键,然后 data.add将匹配值加入到List上;
2、适配器继承BaseAdapter,需添加一个构造方法public MyAdapter(Context context),getView()中用inflate(R.layout.list, null)对一个列表的xml实例成View并返回到convertView,再对对象holder的属性进行赋值,然后用convertView.setTag(holder);
3、最后在ListActivity中用setListAdapter(adapter)完成适配器的映射。