android - Sectioning date header in a ListView that has adapter extended by CursorAdapter -
i trying build demo chatting app.i want show messages section headers dates "today","yesterday","may 21 2015" etc.i have managed achieve since new view method gets called whenever scroll list.the headers , messages mixed up.
for simplicity, have kept header in layouts , changing visibility(gone , visible) if date changes.
can me out this? let me know if needs more info posted in question.
public class chatssadapter extends cursoradapter { private context mcontext; private layoutinflater minflater; private cursor mcursor; private string mmyname, mmycolor, mmyimage, mmyphone; // private list<contact> mcontactslist; private fragmentactivity mactivity; private boolean misgroupchat; public chatssadapter(context context, cursor c, boolean groupchat) { super(context, c, false); mcontext = context; mmycolor = constants.getmycolor(context); mmyname = constants.getmyname(context); mmyimage = constants.getmyimageurl(context); mmyphone = constants.getmyphone(context); misgroupchat = groupchat; mcursor = c; // mactivity = fragmentactivity; /*try { mcontactslist = pinchdb.gethelper(mcontext).getcontactdao().queryforall(); } catch (sqlexception e) { e.printstacktrace(); }*/ } @override public int getitemviewtype(int position) { cursor cursor = (cursor) getitem(position); return getitemviewtype(cursor); } private int getitemviewtype(cursor cursor) { boolean type; if (misgroupchat) type = cursor.getstring(cursor.getcolumnindex(chat.colmn_chat_user)).compareto(mmyphone) == 0; else type = cursor.getint(cursor.getcolumnindex(chat.colmn_from_me)) > 0; if (type) { return 0; } else { return 1; } } @override public view newview(context context, cursor cursor, viewgroup parent) { view v = null; int itemviewtype = getitemviewtype(cursor); if (v == null) { minflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); if (itemviewtype == 0) { v = minflater.inflate(r.layout.row_chat_outgoing, parent, false); } else { v = minflater.inflate(r.layout.row_chat_incoming, parent, false); } } return v; } @override public void bindview(view view, context context, cursor cursor) { viewholder holder = new viewholder(); view v = view; final chat chat = new chat(cursor); boolean fromme = misgroupchat ? chat.getuser().compareto(mmyphone) == 0 : chat.isfrom_me(); if (fromme) { // logged in user's data setting.... holder.chat_name = (styleabletextview) v .findviewbyid(r.id.chat_user_name); holder.chat_time = (styleabletextview) v .findviewbyid(r.id.chat_time); holder.chat_tag = (styleabletextview) v .findviewbyid(r.id.chat_tag); int color = color.parsecolor("#ffffff"); v.setbackgroundcolor(color); holder.chat_name.settext("#you"); holder.chat_time.settext(apputil.geteventtime(chat.gettimestamplong())); // header text setting , process.. holder.chat_header_text = (textview) v.findviewbyid(r.id.header_text); string str_date = apputil.coverttodate(chat.gettimestamplong()); string pref_date = sharepreferencesutil.getsharedpreferencesstring(mcontext, constants.chat_timestamp, ""); if (!str_date.equalsignorecase(pref_date)) { holder.chat_header_text.setvisibility(view.visible); sharepreferencesutil.putsharedpreferencesstring(mcontext, constants.chat_timestamp, str_date); holder.chat_header_text.settext(str_date); } else { holder.chat_header_text.setvisibility(view.gone); } string firstword, therest; string mystring = chat.gettext(); string arr[] = mystring.split(" ", 2); if (arr.length > 1) { firstword = arr[0]; // word hash.. therest = arr[1]; // rest of body.. holder.chat_tag.settext(html.fromhtml("<font color=\"#999999\"><b>" + firstword + "</b></font>" + " " + "<font color=\"#000000\">" + therest + "</font>")); // holder.chat_text.settext(therest); // holder.chat_text.setclickable(false); } else { string msg = arr[0]; // word hash.. holder.chat_tag.settext(html.fromhtml("<font color=\"#999999\"><b>" + msg + "</b></font>")); //holder.chat_text.settext(""); } updatetimetextcolorasperstatus(holder.chat_time, chat.getstatus()); v.settag(holder); } else { // other user's data setting.... holder.chat_name = (styleabletextview) v .findviewbyid(r.id.chat_user_name); holder.chat_time = (styleabletextview) v .findviewbyid(r.id.chat_time); holder.chat_tag = (styleabletextview) v .findviewbyid(r.id.chat_tag); holder.chat_image = (imageview) v .findviewbyid(r.id.chat_profile_image); string image = cursor.getstring(cursor.getcolumnindex("image")); string name = cursor.getstring(cursor.getcolumnindex("name")); string color = cursor.getstring(cursor.getcolumnindex("color")); // set values... if (holder.chat_image != null) { mimageloader.displayimage(context, image, holder.chat_image, r.drawable.round_user_place_holder); } int back_color = color.parsecolor("#ffffff"); v.setbackgroundcolor(back_color); holder.chat_name.settext(name); holder.chat_time.settext(apputil.geteventtime(chat.gettimestamplong())); // header text setting , process.. holder.chat_header_text = (textview) v.findviewbyid(r.id.header_text); string str_date = apputil.coverttodate(chat.gettimestamplong()); string pref_date = sharepreferencesutil.getsharedpreferencesstring(mcontext, constants.chat_timestamp, ""); log.d("eywa", "str date ::::: " + str_date + " pref date :::::: " + pref_date); /*if (!textutils.isempty(pref_date)) { if (!pref_date.contains(str_date)) { holder.chat_header_text.setvisibility(view.visible); sharepreferencesutil.putsharedpreferencesstring(mcontext, constants.chat_timestamp, pref_date + str_date); holder.chat_header_text.settext(str_date); } else { holder.chat_header_text.setvisibility(view.gone); } } else { holder.chat_header_text.setvisibility(view.visible); sharepreferencesutil.putsharedpreferencesstring(mcontext, constants.chat_timestamp, pref_date + str_date); holder.chat_header_text.settext(str_date); }*/ if (!str_date.equalsignorecase(pref_date)) { holder.chat_header_text.setvisibility(view.visible); sharepreferencesutil.putsharedpreferencesstring(mcontext, constants.chat_timestamp, str_date); holder.chat_header_text.settext(str_date); } else { holder.chat_header_text.setvisibility(view.gone); } string firstword, therest; string mystring = chat.gettext(); string arr[] = mystring.split(" ", 2); if (arr.length > 1) { firstword = arr[0]; // word hash.. therest = arr[1]; // rest of body.. holder.chat_tag.settext(html.fromhtml("<font color=\"#999999\"><b>" + firstword + "</b></font>" + " " + "<font color=\"#000000\">" + therest + "</font>")); // holder.chat_text.setclickable(false); } else { string msg = arr[0]; // word hash.. holder.chat_tag.settext(html.fromhtml("<font color=\"#999999\"><b>" + msg + "</b></font>")); // holder.chat_text.settext(""); } string phone = cursor.getstring(cursor.getcolumnindex("user")); final contact contact = new contact(name, phone, "", color, image); if (holder.chat_image != null) { holder.chat_image.settag(contact); // holder.chat_name.settag(contact); holder.chat_image.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { contact con = (contact) v.gettag(); intent intent = new intent(mcontext, otherprofileactivity.class); intent.putextra(constants.extra_contact, con); mcontext.startactivity(intent); } }); } v.settag(holder); } /*else { view= }*/ } private void updatetimetextcolorasperstatus(textview chat_time, int status) { if (status == 0) chat_time.setvisibility(view.invisible); else { chat_time.setvisibility(view.visible); /* if (status == 1) chat_time.settextcolor(mcontext.getresources().getcolor(android.r.color.white));*/ if (status == 2) chat_time.settextcolor(mcontext.getresources().getcolor(android.r.color.darker_gray)); else if (status == 3) chat_time.settextcolor(mcontext.getresources().getcolor(android.r.color.black)); } } @override public int getviewtypecount() { return 2; } public class viewholder { public styleabletextview chat_name; public styleabletextview chat_time; public styleabletextview chat_tag; public imageview chat_image; public textview chat_header_text; } @override public int getcount() { if (getcursor() == null) { return 0; } else { return getcursor().getcount(); } } }
Comments
Post a Comment