0

I have my custom adapter as follow. My aim is to display, upcoming & past appointments in a one ListView. So I decided to go with Custom ArrayAdapter. But I have no clue why getView() is not getting called. Also overridden getCount() gives 50 as size of 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;
    }

}`
hemu
  • 3,199
  • 3
  • 44
  • 66

3 Answers3

2

Hey please check your Custom Adapters Constructor where you are passing 0. There you should pass list items object to Super Class Constructor. as per your code that is the mistake I found.

if you required two different views then you are doing wrong, you should override getViewTypeCount() and getItemViewType() in the AdapterClass to display two different views in ListView.

Instead of checking InSection you can call getItemViewType() in getView() to determine which view is required.

Have reference at below links.

Reusing views in Android Listview with 2 different layouts

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

Community
  • 1
  • 1
TNR
  • 5,839
  • 3
  • 33
  • 62
  • But my list item is not fixed. It can be section item (Like "Upcoming Appointments" section or "Past Appointment Section") or the actual list item. – hemu Dec 10 '12 at 12:18
  • Any link to full example/explanation to `getItemViewType()` and `getItemViewType()`...??? – hemu Dec 10 '12 at 12:29
  • I have added links to the Answer – TNR Dec 10 '12 at 12:32
0

Its only because your getCount may return zero.. Check the return value of getCount which is default overriden method , you have to return the count of your array adapter to this.Preveiously it was calling the supers getCount which is zero. Plz override it

0

Your adapter looks fine, I think we can't help you without see the adapter relative code in your activity.

But ensure you don't forget to call yourAdapter.notifyDataSetChanged() after any modifications on the item list.

FabiF
  • 2,796
  • 2
  • 18
  • 27