【问题标题】:How to retrieve and use the actual ID of a recyclerView Item from the database如何从数据库中检索和使用recyclerView Item的实际ID
【发布时间】: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&lt;ClassesBean&gt; getAllSectionsByClassesID() 行的数据类型,我收到错误 incompactable datatypes

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

【问题讨论】:

  • 你好@Aks4125 你对我的问题有什么解决方案吗? .
  • 你遇到过这样的问题@Aks4125
  • 你好 Aham_Uzoma。我可以帮助你,但是你的问题太长太复杂了,没有人回答这么长的问题,幸运的是,我已经阅读了整本书;D。我正在为你写一个答案
  • 你好。 @Shahin 非常感谢您抽出宝贵的时间。我非常感谢您的回复。我注意到了你的建议

标签: android sql sqlite android-recyclerview


【解决方案1】:

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

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

我推荐的是((为两组信息引入一组 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

【讨论】:

  • 好的。@Shahin 仍在测试这个。我会尽快根据结果发布我的回放。但是我的数据库getAllSectionsByClassesID() 和表定义怎么样?
  • 你能告诉我你在每个 CardView 中使用了两个单独的 RecyclerView,对吗???
  • 因为如果你是,你的问题可以在你的活动类中解决。我不知道你在那里做了什么。但我认为它应该比这个答案更容易解决
  • 两个独立的回收站视图。不。@Shahin 我为我的卡片视图实现了一个可扩展的功能,所以它可以在点击时展开
猜你喜欢
  • 1970-01-01
  • 2014-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-11
  • 2022-06-13
  • 2017-08-25
  • 1970-01-01
相关资源
最近更新 更多