[英]Switch changes its behaviour when there are more than 5 items in RecyclerView
I have recyclerView showing list of items from database. 我有recyclerView显示数据库中的项目列表。 In every item there is a switch showing value true
or false
depending on database value. 在每个项目中,都有一个开关,根据数据库值显示值true
或false
。 If you change switch state (eg turn it on/off) it will update value in database with current boolean. 如果您更改开关状态(例如打开/关闭),它将使用当前布尔值更新数据库中的值。 What is the problem, that it works well when there are up to 5 items in RecyclerView. 问题是,当RecyclerView中最多包含5个项目时,它可以很好地工作。 If there are more than 5 items Switches of all items change its state to on/randomly and updating the row in database. 如果有5个以上的项目,则所有项目的开关会将其状态更改为“开/随机”,并更新数据库中的行。 When I tested by showing just Toast with current state it all worked fine. 当我通过仅显示Toast的当前状态进行测试时,一切正常。
I tested on 2 databases with same result: both sqlite3
with ContentProviders and now I migrated to Room with LiveData. 我在2个数据库上进行了测试,结果相同:两个sqlite3
和ContentProviders一起使用,现在我迁移到Room和LiveData。
This is how I set onCheckedChanged
in my adapter: 这是我在适配器中设置onCheckedChanged
:
@NonNull
@Override
public AlarmViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.alarm_item, parent, false);
return new AlarmViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull AlarmViewHolder holder, int position) {
//get data for current adapter position
Alarms currentData = mAlarms.get(position);
//check if alarm is valid and then set this value to the switch
holder.isAlarmActiveSwitch.setChecked(currentData.isValid());
new validationUpdater().switchChangedListener
(holder.isAlarmActiveSwitch,
context, currentData.get_id());`
}
I create instance of validationUpdater()
class and call the method switchChangedListener
, pass holder.isAlarmActiveSwitch
as Switch, context
and id
of row in Database. 我创建了switchChangedListener
validationUpdater()
类的实例,并调用方法switchChangedListener
,将holder.isAlarmActiveSwitch
传递为Switch,数据库的context
和行的id
。
switchChangedListener()
method: switchChangedListener()
方法:
public void switchChangedListener(@NotNull final Switch switchActive, @NotNull final Context context, final long id) {
switchActive.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(final CompoundButton compoundButton, final boolean b) {
AsyncTask.execute(new Runnable() {
@Override
public void run() {
AlarmDatabase db = AlarmDatabase.getInstance(context);
AlarmsDao dao = db.alarmsDao();
dao.updateAlarm(id, b);
}
});
}
});
}
and @Query from Dao: 和@Dao的查询:
@Query("UPDATE alarm_table SET valid = :valid WHERE _id = :id")
void updateAlarm(final long id, final boolean valid);
Could anyone explain me why this doesn't work as supposed only after more than 5 items are displayed? 谁能解释我为什么只有在显示了5个以上的项目后才能按预期运行?
your problem is row item duplication in recycler view overried the getItemViewType method, hope this works for you. 您的问题是回收站视图中的行项目重复覆盖了getItemViewType方法,希望这对您有用。
@Override
public int getItemViewType(int position) {
return position;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.