2016-01-09 2 views
0

Я пытаюсь добавить разделители между моими элементами списка, я отсортировал их в порядке дат и добавил записи в список, где разделителю нужно идти, но как только он доходит до разделителя, он останавливается, нет сообщения об ошибке или что-то, что просто не добавляет разделителя. Я добавил точки останова, и он определенно запускает код, чтобы добавить его, но он не отображается. Даже если после разделителя есть другие элементы, они все равно останавливаются на разделителе.Добавление разделителей в listView

код:

@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; 
    } 
+0

Вы должны быть более обеспокоены тем, как правильно реализовать 'GetView()' – Emmanuel

+0

использовать спросил здесь делитель, http://stackoverflow.com/q/3979218/794088 – petey

+0

@petey разве делитель не идет после каждого предмета? я хочу, чтобы мой список просмотрел по месяцам с заголовками за каждый месяц – Jack

ответ

1

Создать Coustom адаптер, как это.

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.

Смежные вопросы