0

我的自定义适配器如下。我的目标是在一个中显示,即将到来的和过去的约会ListView。所以我决定一起去Custom ArrayAdapter。但我不知道为什么 getView() 没有被调用。也覆盖getCount()了 50 作为大小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;
    }

}`
4

3 回答 3

2

嘿,请检查您传递 0 的自定义适配器构造函数。在那里您应该将列表项对象传递给超类构造函数。根据您的代码,这是我发现的错误。

如果您需要两个不同的视图,那么您做错了,您应该覆盖 AdapterClass 中的 getViewTypeCount() 和 getItemViewType() 以在 ListView 中显示两个不同的视图。

您可以在 getView() 中调用 getItemViewType() 来确定需要哪个视图,而不是检查 InSection。

在下面的链接中有参考。

在具有 2 种不同布局的 Android Listview 中重用视图

http://logc.at/2011/10/10/handling-listviews-with-multiple-row-types/

于 2012-12-10T12:16:20.820 回答
0

只是因为您的 getCount 可能返回零。检查 getCount 的返回值,这是默认的覆盖方法,您必须将数组适配器的计数返回给 this.Preveiously 它正在调用为零的超级 getCount。请覆盖它

于 2013-08-30T10:05:35.410 回答
0

您的适配器看起来不错,我认为如果没有在您的活动中看到适配器相关代码,我们将无法帮助您。

但请确保您不要忘记yourAdapter.notifyDataSetChanged()在对项目列表进行任何修改后调用。

于 2013-08-30T10:27:31.740 回答