在我的应用程序中,我使用了循环视图和游标适配器,以便将信息发布到列表视图格式。
我面临的当前问题是我尝试在下面的代码中设置一个视图持有者,以便当用户更改开关的状态时,它会将其存储在视图持有者中。
目前,在我的代码中,只要用户选择了交换机,它就会将数据库值更改为true或false,以使交换机正常工作。但是当用户向上或向下滚动时,开关将重置。
我想知道是否有人在实现视图持有者存储开关状态方面有任何建议。
public AlarmRecyclerAdapter(Context context, final Cursor cursor) {
mContext = context;
mCursorAdapter = new CursorAdapter(mContext, cursor, 0) {
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
rootView = LayoutInflater.from(context).inflate(R.layout.temp_layout3, parent, false);
return rootView;
}
@Override
public void bindView(View view, final Context context, final Cursor cursor) {
mView = view;
mCursor = cursor;
/* My layout views */
custom_alarm_switch = (Switch) view.findViewById(R.id.selectedContacts);
/* Get information from database */
final String switchState = cursor.getString(cursor.getColumnIndexOrThrow("switch_state"));
position = cursor.getPosition();
/* Set information */
custom_alarm_switch.setChecked(Boolean.valueOf(switchState));
cursor.moveToPosition(position);
}
public static class Viewholder extends RecyclerView.ViewHolder {
TextView header;
Switch custom_alarm_switch;
public Viewholder(View itemView) {
super(itemView);
custom_alarm_switch = (Switch) itemView.findViewById(R.id.selectedContacts);
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mCursorAdapter.newView(mContext, mCursorAdapter.getCursor(), parent);
return new Viewholder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
mCursorAdapter.getCursor().moveToPosition(position);
mCursorAdapter.bindView(holder.itemView, mContext, mCursorAdapter.getCursor());
}
@Override
public int getItemCount() {
return mCursorAdapter.getCount();
}