我有一个列表,当用户点击该列表中的项目时,将在该项目下显示三个按钮(即是,否和可能),因此用户可以选择任何一个按钮和该响应(如1,2, 3是for,no,maybe)将被保存。在适配器的getView中,我们只检索该响应并在所有列表项的行上显示imageView
在ArrayAdapter的getView中设置图像
EventUserRelationMO eventUserRelationMO1 =new EventUserRelationMO();
eventId1 = eventUserRelationMO1.getEventId();
if (eventId1 == eventIdPosition) {
long eventUserId = eventUserRelationMO1.getEventUserId();
int isAttending = eventUserRelationMO1.getIsAttending();
if (isAttending == 1) {
holder.no.setVisibility(View.GONE);
holder.maybe.setVisibility(View.GONE);
} else if (isAttending == 2) {
holder.yes.setVisibility(View.GONE);
holder.maybe.setVisibility(View.GONE);
} else if (isAttending == 3) {
holder.yes.setVisibility(View.GONE);
holder.no.setVisibility(View.GONE);
} else {
holder.yes.setVisibility(View.GONE);
holder.no.setVisibility(View.GONE);
holder.maybe.setVisibility(View.GONE);
}
}
这是完美无缺的,但当我在列表中有更多项目时,相应的项目不可见/滚动时随机播放
private class UserOccasions extends ArrayAdapter<EventMO> {
private ArrayList<EventMO> eventMOs;
LayoutInflater mInflater;
UserOccasions(Context context, int textViewResourceId,
ArrayList<EventMO> eventMOs) {
super(context, textViewResourceId, eventMOs);
this.eventMOs = new ArrayList<EventMO>();
this.eventMOs.addAll(eventMOs);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public class ViewHolder {
TextView eventNameTxtV;
TextView eventPlaceTxtV;
TextView sendByNameTxtv;
ImageView yes;
ImageView no;
ImageView maybe;
}
@Override
public int getCount() {
return eventMOs.size();
}
/*@Override
public Object getItem(int position) {
return position;
}*/
@Override
public long getItemId(int position) {
return 0;
}
// show list values name and mobile number in contact page
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
final EventMO eventMO = eventMOs.get(position);
if (null == convertView) {
convertView = mInflater.inflate(R.layout.invitation, null);
holder = new ViewHolder();
holder.eventNameTxtV = (TextView) convertView.findViewById(R.id.invitationTitle);
holder.eventPlaceTxtV = (TextView) convertView.findViewById(R.id.invitationCheckBox);
holder.sendByNameTxtv = (TextView) convertView.findViewById(R.id.sendByName);
holder.yes = (ImageView) convertView.findViewById(R.id.yes);
holder.no = (ImageView) convertView.findViewById(R.id.no);
holder.maybe = (ImageView) convertView.findViewById(R.id.maybe);
eventUserRelationMOs = eventMO.getEventUserRelationBOs();
sendUserMO = eventMO.getUserBO();
eventIdPosition = eventMO.getEventId();
for (EventUserRelationMO eventUserRelationMO1 : eventUserRelationMOs) {
eventId1 = eventUserRelationMO1.getEventId();
if (eventId1 == eventIdPosition) {
long eventUserId = eventUserRelationMO1.getEventUserId();
int isAttending = eventUserRelationMO1.getIsAttending();
if (isAttending == 1) {
holder.no.setVisibility(View.GONE);
holder.maybe.setVisibility(View.GONE);
} else if (isAttending == 2) {
holder.yes.setVisibility(View.GONE);
holder.maybe.setVisibility(View.GONE);
} else if (isAttending == 3) {
holder.yes.setVisibility(View.GONE);
holder.no.setVisibility(View.GONE);
} else {
holder.yes.setVisibility(View.GONE);
holder.no.setVisibility(View.GONE);
holder.maybe.setVisibility(View.GONE);
}
}
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.yes.setTag(eventMOs.get(position));
holder.no.setTag(eventMOs.get(position));
holder.maybe.setTag(eventMOs.get(position));
holder.eventNameTxtV.setText(eventMOs.get(position).getText());
holder.eventPlaceTxtV.setText(eventMOs.get(position).getPlace());
holder.sendByNameTxtv.setText(sendUserMO.getUserName());
View v = convertView.findViewById(R.id.invitation_single);
v.getRootView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent occasionAct = new Intent(InvitationFragment.this.getActivity(), InvitationActivity.class);
occasionAct.putExtra("eventMO", eventMO);
occasionAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(occasionAct);
}
});
return convertView;
}
这是我的完整适配器代码,任何人帮我在滚动时为列表中的每个项目维护相应的imageView
答案 0 :(得分:0)
ListView通过回收视图来工作。要了解它的工作原理,你可以看一下(http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/)。
代码的问题是当convertView为null时,图像只被设置一次。滚动列表时,列表视图会将循环行传递给getView函数(这时convertView不会为null)。在这种情况下,您没有设置ImageView。
我修改了代码,这应该可行
// show list values name and mobile number in contact page
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
final EventMO eventMO = eventMOs.get(position);
if (null == convertView) {
convertView = mInflater.inflate(R.layout.invitation, null);
holder = new ViewHolder();
holder.eventNameTxtV = (TextView) convertView.findViewById(R.id.invitationTitle);
holder.eventPlaceTxtV = (TextView) convertView.findViewById(R.id.invitationCheckBox);
holder.sendByNameTxtv = (TextView) convertView.findViewById(R.id.sendByName);
holder.yes = (ImageView) convertView.findViewById(R.id.yes);
holder.no = (ImageView) convertView.findViewById(R.id.no);
holder.maybe = (ImageView) convertView.findViewById(R.id.maybe);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
eventUserRelationMOs = eventMO.getEventUserRelationBOs();
sendUserMO = eventMO.getUserBO();
eventIdPosition = eventMO.getEventId();
for (EventUserRelationMO eventUserRelationMO1 : eventUserRelationMOs) {
eventId1 = eventUserRelationMO1.getEventId();
if (eventId1 == eventIdPosition) {
long eventUserId = eventUserRelationMO1.getEventUserId();
int isAttending = eventUserRelationMO1.getIsAttending();
if (isAttending == 1) {
holder.no.setVisibility(View.GONE);
holder.maybe.setVisibility(View.GONE);
} else if (isAttending == 2) {
holder.yes.setVisibility(View.GONE);
holder.maybe.setVisibility(View.GONE);
} else if (isAttending == 3) {
holder.yes.setVisibility(View.GONE);
holder.no.setVisibility(View.GONE);
} else {
holder.yes.setVisibility(View.GONE);
holder.no.setVisibility(View.GONE);
holder.maybe.setVisibility(View.GONE);
}
}
}
holder.yes.setTag(eventMOs.get(position));
holder.no.setTag(eventMOs.get(position));
holder.maybe.setTag(eventMOs.get(position));
holder.eventNameTxtV.setText(eventMOs.get(position).getText());
holder.eventPlaceTxtV.setText(eventMOs.get(position).getPlace());
holder.sendByNameTxtv.setText(sendUserMO.getUserName());
View v = convertView.findViewById(R.id.invitation_single);
v.getRootView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent occasionAct = new Intent(InvitationFragment.this.getActivity(), InvitationActivity.class);
occasionAct.putExtra("eventMO", eventMO);
occasionAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(occasionAct);
}
});
return convertView;
}