“如何正确编辑我的Recycler适配器中的项目?

时间:2019-01-22 22:32:28

标签: java android sqlite android-recyclerview

当我编辑一个项目时,它会更改recycleview中的所有项目,并且不会刷新[view?]事件。我不知道该怎么办。我认为问题可能在于,根据表中更新语句中的项目位置,它没有获得行ID。怎么了?

以下是详细信息:

public class RecyclerUser extends 
  RecyclerView.Adapter<RecyclerUser.RecyclerViewHolderz> {

    Context context;
    LayoutInflater inflater;
    private List<String> users_names_list;
    UsuariosModel usertModel;
    String batch_name;

    ArrayList<UsuariosModel> student_list;
    private ArrayList<Object> arrayList = new ArrayList<>();
    List<String> student_names;
    List<String> user_id_list;
    private MyDatabase mDatabase;

    public RecyclerUser(Context context, List<String> users_names_list) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.users_names_list = users_names_list;
        student_list = new ArrayList<UsuariosModel>();
        mDatabase = new MyDatabase(context);
        this.notifyDataSetChanged();
        this. modifyItem();
    }

    @Override
    public RecyclerViewHolderz onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = inflater.inflate(R.layout.item_list, parent, false);
        RecyclerViewHolderz viewHolders = new RecyclerViewHolderz(v);

        if (student_list == null) {

        } else {
            for (int i = 0; i < student_list.size(); i++) {
                student_names.add(student_list.get(i).getusuarios_name());
                user_id_list.add(student_list.get(i).getnumero_name());
            }
        }
        return viewHolders;
    }

    @Override
    public void onBindViewHolder(final RecyclerViewHolderz holder,final int position) {
        final String bakery = this.users_names_list.get(position);
        holder.tv1.setTag(position);
        holder.tv1.setText((CharSequence) users_names_list.get(position));

        holder.tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
           AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("Add new Student and its UserId");

                LinearLayout layout = new LinearLayout((context));
                layout.setOrientation(LinearLayout.VERTICAL);
                // Set up the input
                final EditText user_name = new EditText(context);

                // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
                user_name.setInputType(InputType.TYPE_CLASS_TEXT);
                user_name.setHint("User Name");
                layout.addView(user_name);

                final EditText user_id = new EditText(context);

                // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
                user_id.setInputType(InputType.TYPE_CLASS_TEXT);
                user_id.setHint("User Id");
                layout.addView(user_id);

                builder.setView(layout);

                // Set up the buttons
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        usertModel = new UsuariosModel(position,user_name.getText().toString(), user_id.getText().toString(), batch_name);
                        final SQLiteDatabase db = MyDatabase.getInstance(context).getWritableDatabase();
                        UserTable.updateItem(db,usertModel);

                        db.close();

                        //refresh the activity
                        ((Tab_system)context).finish();
                        context.startActivity(((Tab_system)context).getIntent());
                   }
                });

                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                builder.show();

                Toast.makeText(context, holder.tv1.getText().toString() + "\nhola cabezon" + position, Toast.LENGTH_SHORT).show();
            }
        });

        holder.tv1.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                final android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context);
                builder.setTitle("Delete");
                builder.setMessage("Are you sure you want to delete it?");

                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    users_names_list.remove(position);
                    notifyItemRemoved(position);
                    notifyItemRangeChanged(position, users_names_list.size());
                    final SQLiteDatabase db = MyDatabase.getInstance(context).getWritableDatabase();
                    UserTable.deleteByStudentName(db, holder.tv1.getText().toString());
                }
            });

                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
                builder.show();
                return true;
        }
        });
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RecyclerViewHolderz hola = (RecyclerViewHolderz) v.getTag();
            int position = hola.getPosition();
            Toast.makeText(context, "This is position " + position, Toast.LENGTH_LONG).show();
        }
    };

    @Override
    public int getItemCount() {
        return users_names_list.size();
    }

    public void notifyDatasetChanged(List<String> newbowlerlist) {
        student_names.clear();
        student_names.addAll(newbowlerlist);
        super.notifyDataSetChanged();
    }

    public void modifyItem() {
        notifyDataSetChanged();
    }

    private void notifyDataSetChanged(int position) {
        notifyDataSetChanged();
    }

    public class RecyclerViewHolderz extends RecyclerView.ViewHolder {
        TextView tv1;
        ImageView imageView;

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

            imageView = (ImageView) itemView.findViewById(R.id.list_avatar);
            tv1 = (TextView) itemView.findViewById(R.id.student_name_text_view);
        }
    } 
}

更新声明

    public static long updateItem(SQLiteDatabase db, UsuariosModel usuarios) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("Usuario_name", usuarios.getusuarios_name());

        long ver = db.update(
                      TABLE_NAME, 
                      contentValues,
                      ID,
                      null);
        return ver;
    }

这是结果的screenshot! 请帮助

0 个答案:

没有答案