Listview 无法在列表视图中添加分隔符
这是我的密码Listview 无法在列表视图中添加分隔符,listview,android-layout,list-separator,Listview,Android Layout,List Separator,这是我的密码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.email_list_main); emailResults = new ArrayList<GetEmailFromDatabase>(); //int[] colors =
@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 {
这里我比较当前日期和列表视图项日期,这是我的问题,日期匹配,但没有添加分隔符我尝试了很多方法,但都没有效果分隔符的代码如下
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();
}
}
}
}
在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;
}
如果有人知道,请告诉我哪里做错了
Thanx我解决了这个问题,使用另一个xml布局在这个xml布局中所有内容都应该是相同的,但是在这个布局示例文本视图中添加另一个视图,并用不同的名称保存它,现在当你想添加分隔符时,你必须膨胀这个布局,而不是膨胀的常规布局。请记住,当您膨胀此布局时,它将取代适配器中的项,所以跳过该项需要返回到数组中的该位置,并在listview中显示该数据
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;
}
public static class ViewHolder {
public TextView textView;
}