列表三元素:
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)完成適配器的映射。