问题
Hello Everyone I really need your help here. In The App I am Developing, this is what i want to achieve at the moment.
AIM
I am trying to divide a school class into sections. E.g Grade One class will have Grade 1A and Grade 1B, Grade Two class will have Grade 2A and Grade 2B etc.
This is achieved by clicking a plus Button on the UI which initiates a dialog popup with forms to enter the class sections for the class.The user sees the sections he/she has added by clicking the class name(Which is a recyclerview Item) which initiates an expansible card-view
with the details of the class sections added.
RESULT
When I click on class name to see all sections added, I notice that the Class sections is populated for all class name in the recyclerview
as show in the images below. All classes show the same class section irrespective of the class the sections were added.
Code
Here is the
onBindViewHolder
in my adapter class
@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();
}
});
}
Here is getAllSectionsByClassesID() in my Database class. It Joins Both the Sections table and the Classes table. This Method was commented out in the onBindViewHolder() for reasons explained below.
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;
}
Here is my
getAllSections
Method. which is currently in use in theonBindViewHolder()
Method
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;
}
Here is My Foreign key definition in my database class
// 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" + ")";
How can I separate these sections based on their classes ID?
What am I getting wrong in my Database and Adapter classes? (specifically the getAllSections()
and getAllSectionsByClassesID()
methods).
The getAllSectionsByClassesID()
is meant to separate/group the classes sections by their classes Id but when I implement it as follows at the line (SectionsBean s : demeaSQL.getAllSectionsByClassesID())
I get an error incompactable datatypes
because of the datatype at the Line public List<ClassesBean> getAllSectionsByClassesID()
I will appreciate a workable code or a better implementation of this concept. Thank you
回答1:
I Guess, I'm wring a Book as well. Just Read Carefully.
I'm gonna Make My Point by Simpler Example.
what I Recommend is to ((INTRODUCE A SET OF ID FOR BOTH SETS OF INFORMATION))
So, Let's say we have 10 Row of Grades, And I'm Just Gonna Create a Class to hold This Information:
public class MyData {
public int getIds(int pickID) {
int[] SetOfIDs = new int[] {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
};
return SetOfIDs[pickID];
}
public int getCount() {
return 10;
}
//------- We Should Follow This with Our Actual Data
}
Right??
Now, Your String Set for Grades should Follow the Same Number. I'm Just Gonna Use a Simple String Set inside This Class I Made:
public class MyData {
public int IDs(int pickID) {
int[] SetOfIDs = new int[] {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
};
return SetOfIDs[pickID];
}
public int IdsCount() {
return 10;
}
// -- THIS WOULD BE OUR GRADE DATA
public String getGrades(int pickGrade) {
String[] grades = new String[] {
"Grade0",
"Grade1",
"Grade2",
"Grade3",
"Grade4",
"Grade5",
"Grade6",
"Grade7",
"Grade8",
"Grade9"
};
return grades[pickGrade];
}
}
So, Before we Move on To Our RecyclerView. We Need to Manage this Data Before The RecyclerView Adapter Is being Created:
Let's say For A Single Item we need IDs And Grade Data:
public class ITEMS{
String MyGrade;
int MyId;
}
So, Now We Use This Item Class to Populate Our RecyclerView. I'm Just Gonna Use a Simple For Loop to populate:
RecyclerView recyclerView;
RecyclerViewAdapter adapter;
private ArrayList < ITEMS > items = new ITEMS < > ();
...
...
MyData data = new MyData();
for (int i = 0; i < data.getCount(); ++i) {
items item = new items();
item.IDs = data.getIds(i);
item.Grades = data.getGrades(i);
items.add(item);
}
// ---- Happening Before RecyclerViewAdapter
...
// --- NOW AFTER THAT INITIATE YOUR ADAPTER
adapter = new (.....);
recyclerView.setAdapter(adapter);
...
Now, What Is Happening Inside Your Adapter onBind
Method is This:
...
private ArrayList <ITEMS> items = new ITEMS<>();
...
@Override
public void onBindViewHolder(final frag0Recycler.myViewHolder holder, final int position) {
Your_TextView.SetText(items.get(position).MyGrade);
}
...
Now You Can use items.get(position).MyId
AS ID FOR ITEMS AnyWhere Which Is an Integer Value.
Now You Have TWO SETS OF GRADE String? Create Two Different Classes of Grades & Ids for Each One You will Have a Set of IDs
来源:https://stackoverflow.com/questions/49048838/how-to-retrieve-and-use-the-actual-id-of-a-recyclerview-item-from-the-database