Ali*_*Ali 11 java sqlite android android-recyclerview
我将使用RecyclerView作为节标题
我想在SQLite数据库中插入每个日期和时间数据时创建节头
我在下面的链接中找到了这个解决方案,但没有成功
请参考下图
对于使用以下代码的上述图像数据或部分是静态的:
List<Company> childList = new ArrayList<>();
List<CompanySectionHeader> sectionHeaders = new ArrayList<>();
childList = myDb.getAllCompany();
sectionHeaders.add(new CompanySectionHeader(childList, "WEDNESDAY 4 APRIL", 1));
Run Code Online (Sandbox Code Playgroud)
现在,如果我输入今天的数据,那么我创建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));
Run Code Online (Sandbox Code Playgroud)
以下代码是我的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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
下面是我的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;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何动态创建节标题
如果你需要任何代码或信息,你可以问我:)
提前致谢 :
我最近这样做是为了按月对一些东西进行排序。它通过重写 ViewType Function 。
为此,您必须使用类似于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.
}
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以使用多种视图保持器类型。就像这个例子。
希望这可以帮助您解决您的问题
| 归档时间: |
|
| 查看次数: |
1514 次 |
| 最近记录: |