【问题标题】:How to work with recyclerview item IDs in Adapter class如何在 Adapter 类中使用 recyclerview 项目 ID
【发布时间】:2018-03-02 12:37:32
【问题描述】:

过去三天我一直试图解决这个问题,但无济于事。我正在从连接在一起的两个数据库表中检索数据(表名是“类”和“节”)。下图描述了该问题。

每个学校班级(一年级,二年级等)应显示每个被点击班级的部分(一年级班级1A级,二年级班级2A级等)。但从图像中,所有类都显示相同(类部分),而与单击的类无关。

问题

如何使每个部分显示在各自的类中? 示例代码将不胜感激。如果有办法获取 recyclerViewItem 的 Id 并将其与 classes 表中类名的 Id 匹配,我将感谢代码示例。

这是我在适配器类的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) {
            // still thinking on how to apply this(this gets the ItemId of the recyclerview Items)
            long itemId = getRVItemId(position);  
            Log.i("itemId", String.valueOf(itemId));
            if (holder.expandableCardView.isShown()) {
                holder.expandableCardView.setVisibility(View.GONE);
            } else {
                holder.expandableCardView.setVisibility(View.VISIBLE);
                if(itemId == listClassesCSOS.get(position).getId()) {
                    StringBuilder sb = new StringBuilder();
                    listClassesCSOS = demeaSQL.getAllSectionsByClassesID();
                    // not used yet
                    sectionsList = demeaSQL.getAllSections();
                     // getAllSections()
                    for (ClassesBean c : listClassesCSOS ){
                        sb.append(c.sectionBeanGetName() + "\n" + "\n");
                        // sets the textView with data from database after a class name has been clicked
                        holder.addedSectionTV.setText(sb.toString()); 
                    }
                }
            }
        }
    });
    holder.add_class_section.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showAddSectionsDialog();
        }
    });
}

【问题讨论】:

  • 你可以使用嵌套的recyclerview,也可以使用headers (section recyclerview)
  • 感谢您的回答@Santanu_Sur 这是如何工作的。很抱歉这样的问题,但我是这个 android 的新手
  • @pskink 是的,我做到了
  • 您可以通过编程方式实现这一点,但由于您是新手,您可以试试这个库..

标签: android sqlite android-recyclerview


【解决方案1】:

在持有人中,您致电demeaSQL.getAllSectionsByClassesID();。您没有设置所选班级的id,因此每个班级都会收到相同的值。

更新

在您的 demeaSQL 类中,您需要新方法 getAllSectionsByClassesID(long id)

然后改变

listClassesCSOS = demeaSQL.getAllSectionsByClassesID();

listClassesCSOS = demeaSQL.getAllSectionsByClassesID(itemId);

【讨论】:

  • 是的,我知道@anatoli。我怎样才能做到这一点。我没有这样做,因为我不知道如何为此设置 ID 的语法。你能帮我更好地实施吗
  • 如果您在onBindViewHolder 中调用listClassesCSOS.get(position),您将收到当前课程。这个类对象应该有自己的 id,您需要检索当前类部分
  • ID会和数据库classes表中Class name的ID匹配吗? @anatoli
  • with itemId yu 应该能够获取所选类的当前值
  • 请您帮助我了解其实现的语法。我一直在尝试,但没有用。我会很感激的。谢谢@anatoli
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-07
  • 2021-07-05
  • 1970-01-01
  • 2021-08-24
相关资源
最近更新 更多