我已根据此link创建了一个示例,该示例在ListView中的几行中具有不同的布局。这是布局
由于我只希望第一行有不同的布局,每当位置为零时我都应用布局一(姓名和电话),其余我应用布局二(地址)。
我遇到的问题是当我在适配器上触发notifyDataSetChanged时,只有我的地址被更改但名称&电话行即第一行不更新。我在选择子菜单项时触发notifyDataSetChanged。
这是代码。
public static final int TYPE_ADDRESS = 1;
public static final int TYPE_NAME = 0;
public static final int TYPE_MAX_COUNT =2;
@Override
public int getItemViewType(int position) {
if(position == 0)
return TYPE_NAME;
else
return TYPE_ADDRESS;
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView + " type = " + type);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ADDRESS:
convertView = mInflater.inflate(R.layout.layout_address, null);
holder.address = (TextView) convertView.findViewById(R.id.address_id);
holder.address.setText(data.getAddress(position));
break;
case TYPE_NAME:
convertView = mInflater.inflate(R.layout.layout_name, null);
holder.name = (TextView)convertView.findViewById(R.id.name_id);
holder.phone = (TextView)convertView.findViewById(R.id.phone_id);
holder.name.setText(data.getName(position));
holder.phone.setText(data.getPhone(position));
break;
}
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
static class ViewHolder {
TextView address;
TextView name;
TextView phone;
}
答案 0 :(得分:0)
AFAIK,您需要在if (convertView == null){}else{}
块之外管理视图(如设置文本),因为您无论如何都需要调用它,而不仅仅是视图创建。这可能是问题吗?
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ADDRESS:
convertView = mInflater.inflate(R.layout.layout_address, null);
holder.address = (TextView) convertView.findViewById(R.id.address_id);
break;
case TYPE_NAME:
convertView = mInflater.inflate(R.layout.layout_name, null);
holder.name = (TextView)convertView.findViewById(R.id.name_id);
holder.phone = (TextView)convertView.findViewById(R.id.phone_id);
break;
}
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
switch (type) {
case TYPE_ADDRESS:
holder.address.setText(data.getAddress(position));
break;
case TYPE_NAME:
holder.name.setText(data.getName(position));
holder.phone.setText(data.getPhone(position));
break;