【发布时间】:2018-03-01 11:33:09
【问题描述】:
大家好,我在这里真的需要你们的帮助。在我正在开发的应用程序中,这是我目前想要实现的目标。
目标
我正在尝试将学校班级划分为多个部分。例如一年级有1A级和1B级,二级有2A级和2B级等。
这是通过单击 UI 上的加号按钮来实现的,该按钮会启动一个对话框弹出窗口,其中包含用于输入类的类部分的表单。用户通过单击类名称来查看他/她添加的部分(这是一个 recyclerview Item),它启动一个可扩展的card-view,并添加了类部分的详细信息。
结果
当我单击类名以查看添加的所有部分时,我注意到类部分已为 recyclerview 中的所有类名填充,如下图所示。所有类都显示相同的类部分,而与添加的部分无关。
代码
这是我的适配器类中的
onBindViewHolder
@Override
public void onBindViewHolder(final ClassSectionOnSetupRecyclerAdapter.ViewHolderCSOS holder, final int position) {
holder.textView_classname_sections.setText(listClassesCSOS.get(position).getClasses_name());
holder.layoutt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
if(holder.expandableCardView.isShown()) {
holder.expandableCardView.setVisibility(View.GONE);
}else{
holder.expandableCardView.setVisibility(View.VISIBLE);
StringBuilder sb = new StringBuilder();
for (SectionsBean s : demeaSQL.getAllSections()){ //getAllSectionsByClassesID()
sb.append(s.getSections_name() + "\n" + "\n");
}
holder.addedSectionTV.setText(sb.toString());
}
}
});
holder.add_class_section.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showAddSectionsDialog();
}
});
}
这是我的数据库类中的 getAllSectionsByClassesID()。它连接 Sections 表和 Classes 表。该方法在 onBindViewHolder() 原因如下所述。
public List<ClassesBean> getAllSectionsByClassesID(){
String[] columns = {
COLUMN_CLASSES_ID,
COLUMN_CLASSES_NAME,
COLUMN_CLASSES_SECTIONS,
COLUMN_CLASSES_SECTIONS_ID,
COLUMN_CLASSES_SECTIONS_NAME,
COLUMN_CLASSES_SECTIONS_DESCRIPTION
};
SQLiteDatabase db = this.getReadableDatabase();
String joinedTables = TABLE_CLASSES + " , " + TABLE_CLASSES_SECTIONS; //I am joining the sections table and Classes table here
//String[] columns = {COLUMN_CLASSES_ID,COLUMN_CLASSES_SECTIONS_ID};
String selection = COLUMN_CLASSES_ID + " = " + COLUMN_CLASSES_SECTIONS_ID;
ArrayList<ClassesBean> sectionsBeanList;
sectionsBeanList = new ArrayList<>();
Cursor cursor = db.query(joinedTables,
columns,
selection,
null,
null,
null,
null,
null);
while (cursor.moveToNext()) {
SectionsBean sectionsBean = new SectionsBean();
sectionsBean.setSectionsID(cursor.getLong(cursor.getColumnIndex(COLUMN_CLASSES_SECTIONS_ID)));
sectionsBean.setSections_name(cursor.getString(cursor.getColumnIndex(COLUMN_CLASSES_SECTIONS_NAME)));
sectionsBean.setSections_description(cursor.getString(cursor.getColumnIndex(COLUMN_CLASSES_SECTIONS_DESCRIPTION)));
ClassesBean classesBean = new ClassesBean();
classesBean.setId(cursor.getLong(cursor.getColumnIndex(COLUMN_CLASSES_ID)));
classesBean.setClasses_sections(String.valueOf(sectionsBean));
sectionsBeanList.add(classesBean);
}
return sectionsBeanList;
}
这是我的
getAllSections方法。目前在onBindViewHolder()方法中使用
public List<SectionsBean>getAllSections(){
ArrayList<SectionsBean> sectionsBeansList = new ArrayList<SectionsBean>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CLASSES_SECTIONS,
null,
null,
null,
null,
null,
null,
null);
while(cursor.moveToNext()){
SectionsBean sectionsBean = new SectionsBean();
sectionsBean.setSectionsID(cursor.getLong(cursor.getColumnIndex(COLUMN_CLASSES_SECTIONS_ID)));
sectionsBean.setSections_name(cursor.getString(cursor.getColumnIndex(COLUMN_CLASSES_SECTIONS_NAME)));
sectionsBean.setSections_description(cursor.getString(cursor.getColumnIndex(COLUMN_CLASSES_SECTIONS_DESCRIPTION)));
sectionsBeansList.add(sectionsBean);
}
cursor.close();
db.close();
return sectionsBeansList;
}
这是我的数据库类中的外键定义
// create classes_table sql query
private String CREATE_CLASSES_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_CLASSES + "("
+ COLUMN_CLASSES_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_CLASS_ITEM_INDEX + " NUMBER,"
+ COLUMN_CLASSES_NAME + " VARCHAR," + COLUMN_CLASSES_CODENAME + " VARCHAR, " + COLUMN_CLASSES_SECTIONS + " INT," + COLUMN_CLASSES_TEACHERS
+ " VARCHAR," + COLUMN_CLASSES_STUDENTS + " VARCHAR," + "FOREIGN KEY(" + COLUMN_CLASSES_SECTIONS + ") REFERENCES "
+ TABLE_CLASSES_SECTIONS + "("+COLUMN_CLASSES_SECTIONS_ID+"));";//"(classes_sections_id) " + ")";
//create sections sql query
private String CREATE_CLASSES_SECTIONS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_CLASSES_SECTIONS + "("
+ COLUMN_CLASSES_SECTIONS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_CLASSES_SECTIONS_NAME + " VARCHAR,"
+ COLUMN_CLASSES_SECTIONS_DESCRIPTION + " VARCHAR" + ")";
如何根据类 ID 将这些部分分开?
我的数据库和适配器类有什么问题? (特别是 getAllSections() 和 getAllSectionsByClassesID() 方法)。
getAllSectionsByClassesID() 旨在通过类 ID 对类部分进行分隔/分组,但是当我在 (SectionsBean s : demeaSQL.getAllSectionsByClassesID()) 行按如下方式实现它时,由于 public List<ClassesBean> getAllSectionsByClassesID() 行的数据类型,我收到错误 incompactable datatypes
我会欣赏一个可行的代码或这个概念的更好实现。谢谢
【问题讨论】:
-
你好@Aks4125 你对我的问题有什么解决方案吗? .
-
你遇到过这样的问题@Aks4125
-
你好 Aham_Uzoma。我可以帮助你,但是你的问题太长太复杂了,没有人回答这么长的问题,幸运的是,我已经阅读了整本书;D。我正在为你写一个答案
-
你好。 @Shahin 非常感谢您抽出宝贵的时间。我非常感谢您的回复。我注意到了你的建议
标签: android sql sqlite android-recyclerview