如何从数据库中检索和使用recyclerView项的实际ID

编程入门 行业动态 更新时间:2024-10-14 20:27:47
本文介绍了如何从数据库中检索和使用recyclerView项的实际ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好,我真的很需要您的帮助.在我正在开发的应用程序中,这是我目前想要实现的目标.

AIM

我正在尝试将学校课程分为几个部分.例如,一年级将具有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()

我将赞赏此概念的可行代码或更好的实现.谢谢

解决方案

我猜,我也在绞书.仔细阅读.

我将通过更简单的例子来说明我的观点.

我推荐的内容((同时为两组信息引入一组ID)

所以,假设我们有10行成绩,而我只是要创建一个类来保存此信息:

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 }

对吗?

现在,您的成绩字符串集应遵循相同的数字.我只是要在我制作的此类中使用一个简单的字符串集:

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]; } }

因此,在我们进入RecyclerView之前.在创建RecyclerView适配器之前,我们需要管理此数据:

比方说,对于单个项目,我们需要ID和成绩数据:

public class ITEMS{ String MyGrade; int MyId; }

因此,现在我们使用此项目类填充我们的RecyclerView.我只是要使用一个简单的For循环来填充:

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); ...

现在,适配器内部发生的事情onBind方法是这样的:

... private ArrayList <ITEMS> items = new ITEMS<>(); ... @Override public void onBindViewHolder(final frag0Recycler.myViewHolder holder, final int position) { Your_TextView.SetText(items.get(position).MyGrade); } ...

现在,您可以在任何为整数值的地方使用items.get(position).MyId AS ID作为项.

现在您有两套GRADE字符串?创建两个不同的等级和等级每个人都有一个ID,您将拥有一组ID

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 the onBindViewHolder() 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

解决方案

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

更多推荐

如何从数据库中检索和使用recyclerView项的实际ID

本文发布于:2023-11-02 03:58:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1551306.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中   recyclerView   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!