android - 使用listview加載不同item,數(shù)據(jù)填充錯(cuò)誤。
問題描述
當(dāng)日期的值為空的時(shí)候,就會(huì)將姓名或郵箱等值填充進(jìn)去,當(dāng)日期有值的時(shí)候現(xiàn)實(shí)正常,找了好久不知道怎么搞,希望各位大神給點(diǎn)建議。
下面為adapter的代碼:
private static final int TYPE_VIEW_COUNT = 2;private static final int TYPE_ONE = 0;private static final int TYPE_TWO = 1;private List<FormData> mDatas;private Context mContext;private LayoutInflater mInflater;private int eventIds = -1;private int attendIds = -1;private int ticketIds = -1;private Attends attends;private EventTicket ticket;private JSONObject object = null;public DetailAdapter(List<FormData> mDatas, Context mContext, int eventId, int attendId, int ticketId) { this.mDatas = mDatas; this.mContext = mContext; this.mInflater = LayoutInflater.from(mContext); this.eventIds = eventId; this.attendIds = attendId; this.ticketIds = ticketId;attends = new Select().from(Attends.class).where(Attends_Table.eventId.is(eventIds)).and(Attends_Table.attendId.is(attendIds)).querySingle(); ticket = new Select().from(EventTicket.class).where(EventTicket_Table.eventIds.is(eventIds)).and(Attends_Table.ticketIds.is(ticketIds)).querySingle(); try {object = new JSONObject(attends.gsonUser); } catch (JSONException e) {e.printStackTrace(); }}@Overridepublic int getItemViewType(int position) { if (position == 0) {return TYPE_ONE; } else {return TYPE_TWO; }}@Overridepublic int getViewTypeCount() { return TYPE_VIEW_COUNT;}@Overridepublic int getCount() { return mDatas.size() + 1;}@Overridepublic Object getItem(int position) { return mDatas.get(position);}@Overridepublic long getItemId(int position) { return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) { DetailViewHolder holder = null; UserDetailViewHolder userDetailViewHolder = null; int viewTypeCount = getItemViewType(position); if (convertView == null) {switch (viewTypeCount) { case TYPE_ONE:userDetailViewHolder = new UserDetailViewHolder();convertView = mInflater.inflate(R.layout.attend_people_datail_type1, null);userDetailViewHolder.tv_name_letter = (CircleTextView) convertView.findViewById(R.id.tv_detail_letter);userDetailViewHolder.tv_detail_name = (TextView) convertView.findViewById(R.id.tv_detail_name);userDetailViewHolder.tv_ticket_name = (TextView) convertView.findViewById(R.id.tv_detail_ticket);convertView.setTag(userDetailViewHolder); // AutoUtils.auto(convertView);break; case TYPE_TWO:holder = new DetailViewHolder();convertView = mInflater.inflate(R.layout.attend_people_datail_type2, null);holder.tv_form = (TextView) convertView.findViewById(R.id.tv_form);holder.tv_detail_content = (TextView) convertView.findViewById(R.id.tv_detail_content);convertView.setTag(holder);} } else {switch (viewTypeCount) { case TYPE_ONE:userDetailViewHolder = (UserDetailViewHolder) convertView.getTag();break; case TYPE_TWO:holder = (DetailViewHolder) convertView.getTag();break;} } //根據(jù)返回類型展示不同的數(shù)據(jù) switch (viewTypeCount) {case TYPE_ONE: if (attends != null && ticket != null) {userDetailViewHolder.tv_name_letter.setText(attends.names.substring(0, 1));userDetailViewHolder.tv_detail_name.setText(attends.names);userDetailViewHolder.tv_ticket_name.setText(ticket.ticketNames); } break;case TYPE_TWO: if (position >= 1) {Log.e('', 'position -->' + position);if (attends != null) { holder.tv_form.setText(mDatas.get(position - 1).getFormName()); try {if (object != null) { holder.tv_detail_content.setText(object.getString(mDatas.get(position - 1).getFormFieldId() + ''));}// Toast.makeText(mContext, holder.tv_form.getText().toString(), Toast.LENGTH_LONG).show(); // Toast.makeText(mContext, holder.tv_detail_content.getText().toString(), Toast.LENGTH_LONG).show(); } catch (JSONException e) {e.printStackTrace(); }} } break; } return convertView;}class DetailViewHolder { TextView tv_form; TextView tv_detail_content;}class UserDetailViewHolder { CircleTextView tv_name_letter; TextView tv_detail_name; TextView tv_ticket_name;}
}
問題解答
回答1:@Overridepublic int getCount() {
return mDatas.size() + 1;
}不需要加一吧