我将使用RecyclerView作为节标题
我想在插入每个日期&时创建节标题SQLite数据库中的时间数据
我在下面的链接中找到了此解决方案但未成功
请参考下图
对于使用以下代码的上述图像数据或部分为静态:
List<Company> childList = new ArrayList<>();
List<CompanySectionHeader> sectionHeaders = new ArrayList<>();
childList = myDb.getAllCompany();
sectionHeaders.add(new CompanySectionHeader(childList, "WEDNESDAY 4 APRIL", 1));
现在如果我输入今天的数据,那么我创建1个截面日期
下面的图像数据部分标题是静态或数据:
以上图片数据正在使用以下代码:
childList.add(new Company("Ketul Inc.", "11/11/2017 3:46 PM"));
childList.add(new Company("Atmel Corporation", "09/19/2017 8:46 PM"));
childList.add(new Company("ABC Technologies", "09/12/2017 7:41 PM"));
childList.add(new Company("Huron Capital Partners LLC", "09/12/2017 7:25 PM"));
sectionHeaders = new ArrayList<>();
//Create a List of SectionHeader DataModel implements SectionHeader
sectionHeaders.add(new CompanySectionHeader(childList, "SATURDAY 7 APRIL", 2));
代码下方是 SectionHeader.Java
:
public class CompanySectionHeader implements Section<Company>, Comparable<CompanySectionHeader> {
List<Company> childList;
String sectionText;
int index;
public CompanySectionHeader(List<Company> childList, String sectionText, int index) {
this.childList = childList;
this.sectionText = sectionText;
this.index = index;
}
@Override
public List<Company> getChildItems() {
return childList;
}
public String getSectionText() {
return sectionText;
}
@Override
public int compareTo(CompanySectionHeader another) {
if (this.index > another.index) {
return -1;
} else {
return 1;
}
}
}
以下是我的SQLite数据库结构:
public String getFromatDate(long dateTime) {
String formatedDate;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(dateTime);
Date mDate = calendar.getTime();
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm a", new Locale("en"));
formatedDate = sdf.format(mDate);
return formatedDate;
}
public long insertCompany(Company company){
//String sql = null;
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DatabaseHelper.KEY_COMPANY_NAME, company.getName());
values.put(DatabaseHelper.KEY_CREATED_AT, System.currentTimeMillis());
values.put(DatabaseHelper.KEY_UPDATED_AT, System.currentTimeMillis());
values.put(DatabaseHelper.KEY_COMPANY_WEBSITE,company.getWebsite());
values.put(DatabaseHelper.KEY_COMPANY_EMAIL,company.getEmail());
values.put(DatabaseHelper.KEY_COMPANY_PHONE_HOME,company.getPhoneHome());
long company_id = db.insert(COMPANY, null, values);
return company_id;
}
我的问题是如何动态创建章节标题
如果您需要任何代码或信息,可以问我:)
提前致谢:
答案 0 :(得分:3)
这里有一个示例Json,你需要做的就是以这种方式保存并获取数据,在Json查看器上运行此示例并查看结构。!
代表SQLIte
public static final String TABLE_NAME = "companies_dates";
public static final String COLUMN_TIMESTAMP = "timestamp"; //primary key
public static final String COLUMN_ID = "id";// primary key
public static final String TABLE_NAME = "companies";
public static final String COLUMN_ID = "id";// primary key
public static final String COLUMN_ID_PARENT = "id_parent"; //foreign key //primary key form table companies_dates
//or use date as parent_date
public static final String COLUMN_DATE_PARENT = "timestamp";
public static final String COLUMN_TITLE = "company";
public static final String COLUMN_TIMESTAMP = "timestamp";
// ResponseMain.Java
public class CompaniesResponse{
ArrayList<Companies> companiesList;
public ArrayList<Companies> getCompaniesList() {
return companiesList;
}
public void setCompaniesList(ArrayList<Companies> companiesList) {
this.companiesList = companiesList;
}
}
public class Companies{
private String timeStamp;
ArrayList<CompaniesItem> companiesArrayList;
public String getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(String timeStamp) {
this.timeStamp = timeStamp;
}
public ArrayList<CompaniesItem> getCompaniesArrayList() {
return companiesArrayList;
}
public void setCompaniesArrayList(ArrayList<CompaniesItem> companiesArrayList) {
this.companiesArrayList = companiesArrayList;
}
}
public class CompaniesItem{
private String title;
private String id;
private String timeStamp;
private String parentId;
private String parentTimeStamp;
//getter setters
}
@Override
public void onCreate(Bundle savedInstanceState) {
//super.onCreate(savedInstanceState);
CompaniesResponse companiesResponse = new CompaniesResponse();
Companies companies = new Companies();
// 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 = new ArrayList<>();
companieslist.add(companies);
companiesResponse.setCompaniesList(companieslist);
}
代表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"
}
]
}
]
}
答案 1 :(得分:1)
创建两个不同的ViewHolder并根据聊天回收器视图like this example等条件对其进行更新。在你的情况下:
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; }
答案 2 :(得分:1)
我最近在每月基础上对事情进行了排序。它通过覆盖ViewType函数。
为此,您必须在Recycler Adapter中使用getViewType()
之类的内容。
public class LastTransAdapter extends RecyclerView.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;
private PopupWindow popUpWindow = null;
private RelativeLayout layout;
private int MONTHHEADER = 1;
private int DATAVIEW = 2;
public LastTransAdapter(ArrayList<LastTransBean> lastTransBeans, Context context, View myView, RelativeLayout layout) {
this.lastTransBeans = lastTransBeans;
this.context = context;
this.myView = myView;
this.layout = layout;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
if (i == DATAVIEW) {
// view for normal data.
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.single_row_last_transactions, viewGroup, false);
return new ViewHolder(view);
} else {
// view type for month or date header
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.single_row_month_header, viewGroup, false);
return new ViewHolder(view);
}
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
final int position = i;
if (viewHolder.getItemViewType() == DATAVIEW) {
//fill data for normal view
} else {
//set your date or month header
}
}
@Override
public int getItemCount() {
return lastTransBeans.size();
}
@Override
public int getItemViewType(int position) {
if (lastTransBeans.get(position).getMonth()) {
return MONTHHEADER;
} else {
return DATAVIEW;
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
MyCustomTextView transtile, transdate, transamont, tv_monthHeader;
LinearLayout acctype;
CardView mlastTransCardView;
public ViewHolder(View itemView) {
super(itemView);
// cast all the textviews , buttonsetc used ion view Holder.
}
}
}
您还可以使用多种视图类型。 Like this example
希望这可以帮助您解决问题
答案 3 :(得分:0)
如果我理解正确,您的挑战是查询SQLite数据库,将记录分组到日期中,并在视图中的各个部分中显示它们。
看看这个:
public String formatDate(long dateTime) {
String formatedDate;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(dateTime);
Date mDate = calendar.getTime();
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("EEEE dd EEEE", new Locale("en"));
formatedDate = sdf.format(mDate);
return formatedDate;
}
SQLiteDatabase db = this.getReadableDatabase();
//select companies, order record by date-time created desc (or asc)
Cursor cursor = db.query(Company.TABLE_NAME, null, null,
null, null, null, DatabaseHelper.KEY_CREATED_AT + " DESC", null);
String day="";
ArrayList<Company> childList= new ArrayList<Company>();
int index = 0;
ArrayList<CompanySectionHeader> sectionHeaders = new ArrayList<>();
if (cursor.moveToFirst()){
//assign the formatted current date to a variable
day = formatDate(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_CREATED_AT)));
String current_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(new Company(cursor.getColumnIndex(DatabaseHelper.KEY_COMPANY_NAME), getFromatDate(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_CREATED_AT))));
//compare with day var
if(!day.equals(current_day)) {
//We are beginning a new day, so add the previous days company to the sectioned list
sectionHeaders.add(new CompanySectionHeader(new ArrayList<Company>(childList), day, ++index));
day=current_day; //this is
childList.clear();
}
}
}
cursor.close();
db.close();
//attach sectionHeaders to view here
我希望它有所帮助。