listviewandroid-layoutlist-separator

Unable to add separator in list view


This is my code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.email_list_main);

        emailResults = new ArrayList<GetEmailFromDatabase>();

        //int[] colors = {0,0xFFFF0000,0};
        //getListView().setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
        //getListView().setDividerHeight(2);

        emailListFeedAdapter = new EmailListFeedAdapter(this, R.layout.email_listview_row, emailResults);
        setListAdapter(this.emailListFeedAdapter);

        getResults();
        if(emailResults != null && emailResults.size() > -1){
            emailListFeedAdapter.notifyDataSetChanged();
        for(int i=0;i< emailResults.size();i++){
            try {

Here I getting email Sent date

                emailListFeedAdapter.add( emailResults.get(i));

                datetime_text1 = emailResults.get(i).getDate();
                formatter1 = new SimpleDateFormat();
                formatter1 = DateFormat.getDateInstance((DateFormat.MEDIUM));

                Calendar currentDate1 = Calendar.getInstance();

                Item_Date1 = formatter1.parse(datetime_text1);
                current_Date1 = formatter1.format(currentDate1.getTime());
                current_System_Date1 = formatter1.parse(current_Date1);

                currentDate1.add(Calendar.DATE, -1);

                yesterdaydate = formatter1.format(currentDate1.getTime());
                yeaterday_Date = formatter1.parse(yesterdaydate);

                currentDate1.add(Calendar.DATE, -2);

                threeDaysback = formatter1.format(currentDate1.getTime());
                Three_Days_Back = formatter1.parse(threeDaysback);

Here I am comparing current date with list view item date, and here is my problem, dates are matching but it is not adding separator I tried in so many ways but nothing worked the code for separator is bellow.

                if(Item_Date.compareTo(current_System_Date)==0){
                    if(index1){

                       emailListFeedAdapter.addSeparatorItem("SEPARATOR");
                       //i--;
                       index1=false;
                    }


                }
                else if(yeaterday_Date.compareTo(Item_Date1)==0){
                    if(index2){
                       emailListFeedAdapter.addSeparatorItem("SEPARATOR");
                       //i--;
                       index2 = false;
                    }

                }
                else if(Item_Date1.compareTo(Three_Days_Back)==0){
                    if(index3){
                       emailListFeedAdapter.addSeparatorItem("SEPARATOR");
                       //i--;
                       index3 = false;
                    }

                }
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
     }
}

In EmailListFeedAdapter

     private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
     public void addSeparatorItem(final String item) {
         //itemss.add(emailResults.get(0));
            // save separator position
            mSeparatorsSet.add(itemss.size() - 1);
            notifyDataSetChanged();
        }
    @Override
    public int getItemViewType(int position) {
         return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
       }
         holder = new ViewHolder();
        switch (type) {

            case TYPE_ITEM:
                 emailView= inflater.inflate(R.layout.email_listview_row, null);

                break;
            case TYPE_SEPARATOR:
                 emailView= inflater.inflate(R.layout.item2, null);
                 holder.textView = (TextView)emailView.findViewById(R.id.textSeparator);
                 emailView.setTag(holder);
                 holder.textView.setText("SEPARATOR");
                break;
        }

Here is ViewHolder class

       public static class ViewHolder {
        public TextView textView;
    }

if anybody knows then please tell me where I am doing wrong.

Thanx


Solution

  • I solved this problem take another xml layout every thing should be same in this xml layout but add another view extra in this layout example Text view and save it with different name, now when ever u want to add separator u have to inflate this layout instead of the regular layout u are inflating. remember that when u inflate this layout it is taking place of the item in your adapter, so your skipping that item you need to go back to that position in array and display that data in listview.