Dynamic Section Header Of Recyclerview Using Current Date&time
Solution 1:
here is a sample Json for you all you have to do is save save and fetch your data in this way, run this sample on Json viewer and see structure.!
for SQLIte
publicstaticfinalStringTABLE_NAME="companies_dates";
publicstaticfinalStringCOLUMN_TIMESTAMP="timestamp"; //primary keypublicstaticfinalStringCOLUMN_ID="id";// primary keypublicstaticfinalStringTABLE_NAME="companies";
publicstaticfinalStringCOLUMN_ID="id";// primary keypublicstaticfinalStringCOLUMN_ID_PARENT="id_parent"; //foreign key //primary key form table companies_dates//or use date as parent_datepublicstaticfinalStringCOLUMN_DATE_PARENT="timestamp";
publicstaticfinalStringCOLUMN_TITLE="company";
publicstaticfinalStringCOLUMN_TIMESTAMP="timestamp";
// ResponseMain.Java
publicclassCompaniesResponse{
ArrayList<Companies> companiesList;
publicArrayList<Companies> getCompaniesList() {
return companiesList;
}
publicvoidsetCompaniesList(ArrayList<Companies> companiesList) {
this.companiesList = companiesList;
}
}
publicclassCompanies{
privateString timeStamp;
ArrayList<CompaniesItem> companiesArrayList;
publicStringgetTimeStamp() {
return timeStamp;
}
publicvoidsetTimeStamp(String timeStamp) {
this.timeStamp = timeStamp;
}
publicArrayList<CompaniesItem> getCompaniesArrayList() {
return companiesArrayList;
}
publicvoidsetCompaniesArrayList(ArrayList<CompaniesItem> companiesArrayList) {
this.companiesArrayList = companiesArrayList;
}
}
publicclassCompaniesItem{
privateString title;
privateString id;
privateString timeStamp;
privateString parentId;
privateString parentTimeStamp;
//getter setters
}
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
//super.onCreate(savedInstanceState);CompaniesResponse companiesResponse = newCompaniesResponse();
Companies companies = newCompanies();
// CompaniesItem companiesItem = new CompaniesItem();//companiesItem.setId("1");// companiesItem.setTimeStamp("Time Stamp");// ArrayList<CompaniesItem> companiesItemArrayList = new ArrayList<>();// companiesItemArrayList.add(companiesItem);
companiesItemArrayList.add(myDb.getAllCompany());
companies.setTimeStamp(myDb.getDate());
companies.setCompaniesArrayList(companiesItemArrayList);
ArrayList<Companies> companieslist = newArrayList<>();
companieslist.add(companies);
companiesResponse.setCompaniesList(companieslist);
}
for Json
{"companies ":[{"date":"12-08.2018","companiesList":[{"title":"Atmel Corporation","date":"11/11/2017 3:46 PM"},{"title":"Ketul Inc.","date":"11/11/2017 3:46 PM"}]},{"date":"09/19/2017 8:46 PM","companiesList":[{"title":"Huron Capital Partners LLC","date":"15-06-2018"},{"title":"ABC Technologies","date":"09/12/2017 7:41 PM"}]}]}
Solution 2:
I had Recently done this for sorting something on Monthly Basis. Its By overriding ViewType Function .
For doing this you have to use something like getViewType()
in Recycler Adapter.
publicclassLastTransAdapterextendsRecyclerView.Adapter<LastTransAdapter.ViewHolder> {
private ArrayList<LastTransBean> lastTransBeans;
private Context context;
private MyCustomTextView textViewTitle, tv_Desc, tv_Date, tv_Amount;
private LinearLayout layout1;
private View customView, myView;
private LayoutParams layoutParams;
privatePopupWindowpopUpWindow=null;
private RelativeLayout layout;
privateintMONTHHEADER=1;
privateintDATAVIEW=2;
publicLastTransAdapter(ArrayList<LastTransBean> lastTransBeans, Context context, View myView, RelativeLayout layout) {
this.lastTransBeans = lastTransBeans;
this.context = context;
this.myView = myView;
this.layout = layout;
}
@Overridepublic ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
if (i == DATAVIEW) {
// view for normal data.Viewview= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.single_row_last_transactions, viewGroup, false);
returnnewViewHolder(view);
} else {
// view type for month or date headerViewview= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.single_row_month_header, viewGroup, false);
returnnewViewHolder(view);
}
}
@OverridepublicvoidonBindViewHolder(ViewHolder viewHolder, int i) {
finalintposition= i;
if (viewHolder.getItemViewType() == DATAVIEW) {
//fill data for normal view
} else {
//set your date or month header
}
}
@OverridepublicintgetItemCount() {
return lastTransBeans.size();
}
@OverridepublicintgetItemViewType(int position) {
if (lastTransBeans.get(position).getMonth()) {
return MONTHHEADER;
} else {
return DATAVIEW;
}
}
publicclassViewHolderextendsRecyclerView.ViewHolder {
MyCustomTextView transtile, transdate, transamont, tv_monthHeader;
LinearLayout acctype;
CardView mlastTransCardView;
publicViewHolder(View itemView) {
super(itemView);
// cast all the textviews , buttonsetc used ion view Holder.
}
}
}
You can also use multiple viewholder types. Like this example.
Hope this may help you to solve your problem
Solution 3:
Create two different ViewHolder's and chnage them by condition like chatting recycler view like this example . in your case :
- create variable String date = ""
- then check section header date is equal if not return header view and update the date to varibale (1st time not match date so show headerview)
- if date match return list_header_item_view
public int getItemViewType(int position) { UserMessage message = (UserMessage) mMessageList.get(position); if (message.getSender().getUserId().equals(SendBird.getCurrentUser().getUserId())) { // If the current user is the sender of the message return VIEW_TYPE_MESSAGE_SENT; } else { // If some other user sent the message return VIEW_TYPE_MESSAGE_RECEIVED; } }
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view; if (viewType == VIEW_TYPE_MESSAGE_SENT) { view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_message_sent, parent, false); return new SentMessageHolder(view); } else if (viewType == VIEW_TYPE_MESSAGE_RECEIVED) { view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_message_received, parent, false); return new ReceivedMessageHolder(view); } return null; }
Solution 4:
if I understand you correctly, your challenge is querying the SQLite db, group the records into dates, and display them in sections in the view.
Take a look at this:
public String formatDate(long dateTime) {
String formatedDate;
Calendarcalendar= Calendar.getInstance();
calendar.setTimeInMillis(dateTime);
DatemDate= calendar.getTime();
SimpleDateFormat sdf;
sdf = newSimpleDateFormat("EEEE dd EEEE", newLocale("en"));
formatedDate = sdf.format(mDate);
return formatedDate;
}
SQLiteDatabasedb=this.getReadableDatabase();
//select companies, order record by date-time created desc (or asc)Cursorcursor= db.query(Company.TABLE_NAME, null, null,
null, null, null, DatabaseHelper.KEY_CREATED_AT + " DESC", null);
String day="";
ArrayList<Company> childList= newArrayList<Company>();
intindex=0;
ArrayList<CompanySectionHeader> sectionHeaders = newArrayList<>();
if (cursor.moveToFirst()){
//assign the formatted current date to a variable
day = formatDate(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_CREATED_AT)));
Stringcurrent_day= formatDate(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_CREATED_AT)));
while(cursor.moveToNext()){
current_day = formatDate(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_CREATED_AT)));
childList.add(newCompany(cursor.getColumnIndex(DatabaseHelper.KEY_COMPANY_NAME), getFromatDate(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_CREATED_AT))));
//compare with day varif(!day.equals(current_day)) {
//We are beginning a new day, so add the previous days company to the sectioned list
sectionHeaders.add(newCompanySectionHeader(newArrayList<Company>(childList), day, ++index));
day=current_day; //this is
childList.clear();
}
}
}
cursor.close();
db.close();
//attach sectionHeaders to view here
I hope it helps.
Post a Comment for "Dynamic Section Header Of Recyclerview Using Current Date&time"