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

问题描述 投票:0回答:1

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

目标

我想把一个学校课分成几个部分。 E.g一年级有1A级和1B级,二年级有2A级和2B级等。

这是通过单击UI上的加号按钮来实现的,该按钮启动一个对话框弹出窗口,其中包含用于输入类的类部分的表单。用户通过单击类名称(这是一个recyclerview项目)来查看他/她添加的部分。启动一个可扩展的card-view,其中添加了类部分的详细信息。

结果

当我点击类名来查看添加的所有部分时,我注意到recyclerview中的所有类名都填充了Class部分,如下图所示。所有类都显示相同的类部分,而不考虑添加部分的类。

All recycler View Items shows Grade One sections onclick

Here is Grade One Sections onClick

这是我的适配器类中的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();
            }
        });

    }

这是我的Database类中的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;
     }

这是我的数据库类中的My Foreign key定义

// 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())行实现它时,我得到一个错误incompactable datatypes,因为行public List<ClassesBean> getAllSectionsByClassesID()的数据类型

我将欣赏一个可行的代码或更好地实现这个概念。谢谢

android sql sqlite android-recyclerview
1个回答
0
投票

我猜,我也在写一本书。请仔细阅读。

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

我推荐的是((为两组信息引入一套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作为项目ID,而不是整数值。

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

© www.soinside.com 2019 - 2024. All rights reserved.