具有光标和onClick侦听器的Recyclerview适配器未更新,并通知功能不起作用

时间:2018-12-16 18:34:06

标签: android android-studio android-recyclerview

我正在使用游标适配器来实现recyclerview, 在recyclerviewAdapter内部,我设置了一个简单的onClickListener,它可以更改列表项视图的颜色并更新数据库 当我单击没有任何更改时,除非重新启动活动 如何从适配器内部刷新回收站

我的recyclerVier适配器:

   public class ItemAdapter extends 
      RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {

    MyUsersDbHelper usersDbHelper;
    private Context mContext;
    private Cursor mCursor;

    public ItemAdapter(Context context, Cursor cursor) {
        mContext = context;
        mCursor = cursor;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.lists_list_item, parent, false);
        return new ItemAdapter.ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ItemViewHolder holder, final int position) {

        if (!mCursor.moveToPosition(position)) {
            return;
        }

        String name = mCursor.getString(mCursor.getColumnIndex(ItemsContract.ItemsEntry.COLUMN_ITEM));
        int state = mCursor.getInt(mCursor.getColumnIndex(ItemsContract.ItemsEntry.COLUMN_STATE));
        String category = mCursor.getString(mCursor.getColumnIndex(ItemsContract.ItemsEntry.COLUMN_CATEGORY));

        holder.itemName.setText(name);
        if (state == 0) {
            holder.container.setBackgroundColor(mContext.getResources().getColor(R.color.white_color));
            holder.itemName.setTextColor(mContext.getResources().getColor(R.color.colorPrimaryDark));
            holder.stateColor.setBackgroundColor(mContext.getResources().getColor(R.color.white_color));
            holder.check.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_check_gray_24dp));
        } else if (state == 1) {
            holder.container.setBackgroundColor(mContext.getResources().getColor(R.color.light_gary));
            holder.itemName.setTextColor(mContext.getResources().getColor(R.color.gray));
            holder.check.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_check_black_24dp));
            switch (category) {
                case "Auto":
                    holder.stateColor.setBackgroundColor(mContext.getResources().getColor(R.color.purble));
                    break;
                case "Bills":
                    holder.stateColor.setBackgroundColor(mContext.getResources().getColor(R.color.light_purple));
                    break;
                case "Health":
                    holder.stateColor.setBackgroundColor(mContext.getResources().getColor(R.color.light_blue));
                    break;
                case "Shop":
                    holder.stateColor.setBackgroundColor(mContext.getResources().getColor(R.color.green));
                    break;
                case "Travel":
                    holder.stateColor.setBackgroundColor(mContext.getResources().getColor(R.color.orange));
                    break;
                case "Work":
                    holder.stateColor.setBackgroundColor(mContext.getResources().getColor(R.color.dark_blue));
                    break;
            }
        }
        holder.container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                usersDbHelper = new MyUsersDbHelper(mContext);
                int modPosition= holder.getLayoutPosition();
                mCursor.moveToPosition(modPosition);
                String name = mCursor.getString(mCursor.getColumnIndex(ItemsContract.ItemsEntry.COLUMN_ITEM));
                int state = mCursor.getInt(mCursor.getColumnIndex(ItemsContract.ItemsEntry.COLUMN_STATE));
                String username = mCursor.getString(mCursor.getColumnIndex(ItemsContract.ItemsEntry.COLUMN_USERNAME));

                if (state == 0) {
                    usersDbHelper.updateItemState(username, name, 1);
                    notifyItemChanged(modPosition);
                    notifyDataSetChanged();
                } else if (state == 1) {
                    usersDbHelper.updateItemState(username, name, 0);
                    notifyItemChanged(modPosition);
                    notifyDataSetChanged();
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mCursor.getCount();
    }

    public void swapCursor(Cursor newCursor) {
        if (mCursor != null) {
            mCursor.close();

        }
        mCursor = newCursor;
        if (newCursor != null) {

            notifyDataSetChanged();
        }
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {

        public LinearLayout container;
        public LinearLayout stateColor;
        public ImageView check;
        public TextView itemName;

        public ItemViewHolder(View itemView) {
            super(itemView);

            container = (LinearLayout) itemView.findViewById(R.id.lists_item_container);
            stateColor = (LinearLayout) itemView.findViewById(R.id.lists_item_state_color);
            check = (ImageView) itemView.findViewById(R.id.lists_item_check);
            itemName = (TextView) itemView.findViewById(R.id.lists_item_name);
        }

    }
}

0 个答案:

没有答案