- public View getView(int position, View convertView, ViewGroup parent) {
- System.out.println("getView::" + position);
- int type = TYPE_SEND;
- try
- {
- type = Integer.parseInt(data.get(position).get("type"));
- } catch (Exception e)
- {
- e.printStackTrace();
- }
- ViewHolder holder = null;
- if (convertView == null)
- {
- System.out.println("getView::convertView is null");
- holder = new ViewHolder();
- switch (type)
- {
- case TYPE_SEND:
- convertView = View.inflate(getBaseContext(),
- R.layout.listitem_send, null);
- holder.text = (TextView) convertView
- .findViewById(R.id.message);
- break;
- case TYPE_RECEIVE:
- convertView = View.inflate(getBaseContext(),
- R.layout.listitem_receive, null);
- holder.text = (TextView) convertView
- .findViewById(R.id.message);
- break;
- case TYPE_PIC:
- convertView = new ImageView(getBaseContext());
- ((ImageView) convertView).setImageResource(R.drawable.icon);
- break;
- }
- convertView.setTag(holder);
- }
- else
- {
- System.out.println("getView::convertView not null");
- holder = (ViewHolder) convertView.getTag();
- }
- if (type != TYPE_PIC)
- {
- String msg = data.get(position).get("content");
- holder.text.setText(msg);
- }
- return convertView;
- }
备注:无论使用几种布局,都必须放在一个holder里,不允许中心new Holder