I went through a lot of articles on this subject of how to separate the items and group them under one header. Of all I liked the idea of making a header visible and invisible depending whether to show it or not. I also know that this magic happens in the bindView/newView method of the custom adapter class. So far so good. Now I am querying the Database for dates, I want items falling under a unique date to be grouped together under a Date heading. (That is I want to group items by date) Is there a way I can check dates in if else conditions (Dates fetched from cursor) and depending on these I group them together. One such article on the subject I found wasThis. However I am not able to understand it completely. For me an Algorithm to do the same would work.
So far this is my code:
private class CurAdapter extends CursorAdapter{
// Context context;
// Cursor c;
public CurAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
// this.context = context;
// this.c= c;
}
@Override
public void bindView(View view, Context context, Cursor c) {
TextView tV = (TextView)view.findViewById(R.id.textView1);
TextView tV1 = (TextView)view.findViewById(R.id.textView2);
TextView tV2 = (TextView)view.findViewById(R.id.textView3);
ImageView iM = (ImageView)view.findViewById(R.id.imageView2);
tV1.setTypeface(tf);
tV2.setTypeface(tf);
tV.setText(dateConvert(c.getString(c.getColumnIndexOrThrow("DateToNotify"))));
String B = c.getString(c.getColumnIndexOrThrow("NotificationFor"));
tV1.setText(c.getString(c.getColumnIndexOrThrow("NotificationDateFor")));
if(B.equalsIgnoreCase("1")){
tV2.setText("Is "+c.getString(c.getColumnIndexOrThrow("NotificationData"))+" XXX");
iM.setBackgroundResource(R.drawable.icon_XXX);
}else if(B.equalsIgnoreCase("2")){
tV2.setText("Is "+c.getString(c.getColumnIndexOrThrow("NotificationData"))+" XXX");
iM.setBackgroundResource(R.drawable.icon_XXX);
}else if(B.equalsIgnoreCase("3")){
tV2.setText("Is "+c.getString(c.getColumnIndexOrThrow("NotificationData"))+" XXX");
iM.setBackgroundResource(R.drawable.icon_XXX);
}else if(B.equalsIgnoreCase("4")){
tV2.setText("Is "+c.getString(c.getColumnIndexOrThrow("NotificationData"))+" XXX");
iM.setBackgroundResource(R.drawable.icon_XXX);
}else if(B.equalsIgnoreCase("5")){
tV2.setText("Is "+c.getString(c.getColumnIndexOrThrow("NotificationData"))+" XXX");
iM.setBackgroundResource(R.drawable.icon_XXX);
}else if(B.equalsIgnoreCase("6")){
tV2.setText("Is "+c.getString(c.getColumnIndexOrThrow("NotificationData"))+" XXX");
iM.setBackgroundResource(R.drawable.icon_XXX);
}else if(B.equalsIgnoreCase("7")){
tV2.setText("Is "+c.getString(c.getColumnIndexOrThrow("NotificationData"))+" XXX");
iM.setBackgroundResource(R.drawable.icon_XXX);
}
}
@Override
public View newView(Context context, Cursor c, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.detail_list, parent, false);
bindView(v, context, c);
return v;
}
}
Essentially I want a way to check what the DateToNotify holds for each row, depending on this I will show of hide the view. Any Hints?
EDIT
So far this is what I have tried, although I get the desired result but scrolling the list messes up everything:
@Override
public void bindView(View view, Context context, Cursor c) {
TextView tV = (TextView)view.findViewById(R.id.textView1);
TextView tV1 = (TextView)view.findViewById(R.id.textView2);
TextView tV2 = (TextView)view.findViewById(R.id.textView3);
ImageView iM = (ImageView)view.findViewById(R.id.imageView2);
tV1.setTypeface(tf);
tV2.setTypeface(tf);
if(tV.getText().toString().equals(dateConvert(c.getString(c.getColumnIndexOrThrow("DateToNotify"))))){
tV.setVisibility(View.INVISIBLE);
}else{
tV.setVisibility(View.VISIBLE);
tV.setText(dateConvert(c.getString(c.getColumnIndexOrThrow("DateToNotify"))));
}
EDIT
Optimized Code as perThis Question on StackOverflow
@Override
public View newView(Context context, Cursor c, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.detail_list, null);
//bindView(v, context, c);
TextView tV = (TextView)view.findViewById(R.id.textView1);
TextView tV1 = (TextView)view.findViewById(R.id.textView2);
TextView tV2 = (TextView)view.findViewById(R.id.textView3);
ImageView iM = (ImageView)view.findViewById(R.id.imageView2);
RelativeLayout RL = (RelativeLayout)view.findViewById(R.id.relative1);
view.setTag(R.id.textView1, tV);
view.setTag(R.id.textView2, tV1);
view.setTag(R.id.textView3, tV2);
view.setTag(R.id.imageView2, iM);
view.setTag(R.id.relative1, RL);
return view;
}
And BindView:
@Override
public void bindView(View view, Context context, Cursor c) {
((TextView) view.getTag(R.id.textView2)).setTypeface(tf);
((TextView) view.getTag(R.id.textView3)).setTypeface(tf);
// RelativeLayout RL =(RelativeLayout)view.findViewById(R.id.relative1);
String Bb = ((TextView) view.getTag(R.id.textView1)).getText().toString();
if(Bb.equals(dateConvert(c.getString(c.getColumnIndexOrThrow("DateToNotify"))))){
Toast.makeText(getActivity(), "If Condition: "+Bb, 10000).show();
((RelativeLayout)view.getTag(R.id.relative1)).setVisibility(View.INVISIBLE);
}else{
((RelativeLayout)view.getTag(R.id.relative1)).setVisibility(View.VISIBLE);
((TextView) view.getTag(R.id.textView1)).setText(dateConvert(c.getString(c.getColumnIndexOrThrow("DateToNotify"))));
}