我有自定义适配器如下。
我的目标是展示即将到来的&过去约会ListView
。所以我决定选择Custom ArrayAdapter
。
但我不知道为什么getView()没有被调用。同样被覆盖的getCount()
的大小为items
public class AppointmentListAdapter extends ArrayAdapter<Item> {
List<Item> items;
private Context context;
private LayoutInflater vi;
public static boolean pastApptSectionDrawn = false;
private int healowUserId, portalUserId;
public AppointmentListAdapter(Context context, List<Item> items, int healowUserId, int portalUserId){
super(context, 0, items);
this.context = context;
this.items = items;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.healowUserId = healowUserId;
this.portalUserId = portalUserId;
}
@Override
public int getCount() {
return this.items.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
System.out.println("getView of AppointmentListAdapter...");
View v = convertView;
final Item item = items.get(position);
if(item != null){
if(item.isSection()){
ApptListSection si = (ApptListSection)item;
String title = si.getTitle();
v = vi.inflate(R.layout.appointment_list_section, null);
v.setOnClickListener(null);
v.setOnLongClickListener(null);
v.setLongClickable(false);
View apptSectionShape = v.findViewById(R.id.apptSectionShape);
TextView apptSectionTitle = (TextView) v.findViewById(R.id.apptSectionTitle);
if(apptSectionShape != null)
apptSectionShape.setBackgroundResource(si.getBgResourceId());
if(apptSectionTitle != null)
apptSectionTitle.setText(si.getTitle());
final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
sectionView.setText(title);
if(Global.NO_UPCOMING_APPT.equals(title)){
final LinearLayout sectionItemLayout = (LinearLayout) v.findViewById(R.id.section_item_layout);
sectionItemLayout.setMinimumHeight(CommonUtilities.getDPEquivalentPixels(this.context, 60F));
sectionItemLayout.setBackgroundColor(Color.WHITE);
sectionItemLayout.setGravity(Gravity.CENTER);
sectionView.setTextSize(18);
sectionView.setTextColor(Color.BLACK);
sectionView.setBackgroundColor(Color.WHITE);
sectionView.setGravity(Gravity.CENTER);
}
}else{
Appointment appt = (Appointment)item;
v = vi.inflate(R.layout.list_items_appointments_loading, null);
final TextView appointmentDate = (TextView) v.findViewById(R.id.appointmentDate);
final TextView practiceName = (TextView) v.findViewById(R.id.txtApptProviderName);
final TextView apptReason = (TextView) v.findViewById(R.id.txtApptReason);
final TextView txtDayOfMonth = (TextView) v.findViewById(R.id.txtDayOfMonth);
final TextView txtMonthYear = (TextView) v.findViewById(R.id.txtMonthYear);
final TextView txtDayOfWeek = (TextView) v.findViewById(R.id.txtDayOfWeek);
final TextView txtApptTime = (TextView) v.findViewById(R.id.txtApptTime);
int colorIndCode;
if(appt.isPastAppointment()){
colorIndCode = Color.parseColor("#e4a83c");
}else{
colorIndCode = Color.parseColor("#69cbd8");
}
txtDayOfMonth.setTextColor(colorIndCode);
txtDayOfWeek.setTextColor(colorIndCode);
if(appointmentDate != null){
Calendar cal = Calendar.getInstance();
Date startDateTime = appt.getStartDateTime();
cal.setTime(startDateTime);
//StringBuilder timeBuilder = new StringBuilder(new SimpleDateFormat("MMM").format(startDateTime)+ " " + cal.get(Calendar.DAY_OF_MONTH)+ ", " + cal.get(Calendar.YEAR)+ " ");
StringBuilder timeBuilder = new StringBuilder("");
int hour = cal.get(Calendar.HOUR);
timeBuilder.append(((hour<10)?"0":"") + hour + ":");
int mins = cal.get(Calendar.MINUTE);
timeBuilder.append(((mins<10)?"0":"") + mins + " ");
timeBuilder.append(cal.get(Calendar.AM_PM) == Calendar.AM ? "AM":"PM");
appointmentDate.setText(timeBuilder.toString());
txtDayOfMonth.setText((cal.get(Calendar.DAY_OF_MONTH)));
String month_year = new SimpleDateFormat("MMM").format(startDateTime) + " " + cal.get(Calendar.YEAR);
txtMonthYear.setText(month_year);
txtDayOfWeek.setText(new SimpleDateFormat("EEEE").format(startDateTime));
txtApptTime.setText(timeBuilder.toString());
}
if(practiceName != null){
practiceName.setText(appt.getUfname() + " " + appt.getUlname());
}
if(apptReason != null){
apptReason.setText(appt.getReason());
}
//Write the code to decide on note & reminder symbol
}
}
return v;
}
}`
答案 0 :(得分:2)
嘿,请检查您的自定义适配器构造函数,并在其中传递0.您应该将列表项对象传递给Super Class Constructor。根据你的代码,这是我发现的错误。
如果你需要两个不同的视图然后你做错了,你应该在AdapterClass中覆盖getViewTypeCount()和getItemViewType()以在ListView中显示两个不同的视图。
您可以在getView()中调用getItemViewType()来确定需要哪个视图,而不是检查InSection。
请参考以下链接。
Reusing views in Android Listview with 2 different layouts
http://logc.at/2011/10/10/handling-listviews-with-multiple-row-types/
答案 1 :(得分:0)
这只是因为你的getCount可能返回零.. 检查getCount的返回值,这是默认的overriden方法,你必须将数组适配器的计数返回到this.Preveiously它调用超级getCount为零。 Plz覆盖它
答案 2 :(得分:0)
您的适配器看起来很好,我想如果没有在您的活动中看到适配器相关代码,我们无法帮助您。
但请确保在对项目列表进行任何修改后不要忘记调用yourAdapter.notifyDataSetChanged()
。