Jack
Jack

Reputation: 387

adding separators in a listView

I'm trying to add separators between my list view items, I have sorted them into date order and added list entries where the separator needs to go but as soon as it gets to a seperator it stops, no error message or anything it just doesn't add the seperator. I've added breakpoints and it definitely runs the code to add it but it doesn't show up. Even if there are other items to add after the separator it still stops at the separator.

code:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater itemInflater = LayoutInflater.from(getContext());
    JourneyItem singleItem = list.get(position);

    if(singleItem.isSeperator()){
        //set customRow to seperator layout
        View customRow = itemInflater.inflate(R.layout.journey_list_seperator, parent, false);
        TextView monthText = (TextView) customRow.findViewById(R.id.seperatorMonthText);
        TextView yearText = (TextView) customRow.findViewById(R.id.seperatorYearText);
        Date current = new Date();
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        String dtp = GeneralUtil.SQLDateFormatToHuman(list.get(position).getDepartDateTime());
        try {
            current = df.parse(dtp);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat sdfDate = new SimpleDateFormat("MMMM");

        monthText.setText(sdfDate.format(current));
        yearText.setText(list.get(position).getDepartDateTime().substring(6,10));
        return customRow;
    }else {
        View customRow = itemInflater.inflate(R.layout.custom_journeyitem_row, parent, false);


        TextView titleText = (TextView) customRow.findViewById(R.id.titleDisplay);
        TextView fromText = (TextView) customRow.findViewById(R.id.fromLocationDisplay);
        TextView departText = (TextView) customRow.findViewById(R.id.departDateTimeDisplay);
        TextView toText = (TextView) customRow.findViewById(R.id.toLocationDisplay);
        TextView colourLbl = (TextView) customRow.findViewById(R.id.colourDisplay);

        titleText.setText(singleItem.getTitle());

        fromText.setText("From: " + singleItem.getFromLocation());
        departText.setText(singleItem.getDepartDateTime());
        toText.setText("To: " + singleItem.getToLocation());
        colourLbl.setBackgroundColor(singleItem.getColourCode());
        return customRow;
    }

Upvotes: 0

Views: 412

Answers (1)

Sohail Zahid
Sohail Zahid

Reputation: 8149

Create Coustom adapter like this.

class CustomAdapter extends BaseAdapter {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;

    private ArrayList<String> mData = new ArrayList<String>();
    private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();

    private LayoutInflater mInflater;

    public CustomAdapter(Context context) {
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addItem(final String item) {
        mData.add(item);
        notifyDataSetChanged();
    }

    public void addSectionHeaderItem(final String item) {
        mData.add(item);
        sectionHeader.add(mData.size() - 1);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public String getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int rowType = getItemViewType(position);

        if (convertView == null) {
            holder = new ViewHolder();
            switch (rowType) {
            case TYPE_ITEM:
                convertView = mInflater.inflate(R.layout.snippet_item1, null);
                holder.textView = (TextView) convertView.findViewById(R.id.text);
                break;
            case TYPE_SEPARATOR:
                convertView = mInflater.inflate(R.layout.snippet_item2, null);
                holder.textView = (TextView) convertView.findViewById(R.id.textSeparator);
                break;
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.textView.setText(mData.get(position));

        return convertView;
    }

    public static class ViewHolder {
        public TextView textView;
    }

}

Complete link here.

Upvotes: 1

Related Questions