Long OnClick侦听器删除所选项目

时间:2018-02-18 10:59:17

标签: java android arrays

我有一个活动,我检索用户传达的消息。我想在长按一下删除所选邮件。

这是我的代码,复制到剪贴板功能正常,但删除给我带来了困难。

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MyMessageViewHolder>{


private ArrayList<Messages> arrayListMessages= new ArrayList<>();
private Context mcontext;
FirebaseAuth mAuth;
private DatabaseReference mUserDatabase;

public ArrayAdapter adapter;

public MessageAdapter(Context context, ArrayList<Messages> arrayListMessages){
    this.arrayListMessages = arrayListMessages;
    mcontext = context;
}
@Override
public MyMessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_single_layout, parent,false);

    return new MyMessageViewHolder(view);
}

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

    mAuth = FirebaseAuth.getInstance();
    String mCurrentUser = mAuth.getCurrentUser().getUid();

    final Messages messages = arrayListMessages.get(position);
    String message_type = messages.getType();
    String from_user = messages.getFrom();


    mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(from_user);

    if(mCurrentUser.equals(messages.getFrom())){
        holder.textViewMessage.setBackgroundResource(R.drawable.custom_message_bg_primary);
        holder.textViewMessage.setTextIsSelectable(true);
        holder.textViewMessage.setGravity(Gravity.RIGHT);
        holder.textViewMessage.setTextColor(Color.WHITE);

        holder.textViewMessage.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view)
            {
                CharSequence options[] = new CharSequence[]{"Delete", "Copy Text"};
                final AlertDialog.Builder builder = new AlertDialog.Builder(mcontext);
                builder.setTitle("Select Options");
                builder.setItems(options, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        //Click Event for each item.
                        if (i == 0) {

                            String selectedItem = messages.getMessage();
                            arrayListMessages.remove(selectedItem);


                            Toast.makeText(mcontext, "Message deleted", Toast.LENGTH_SHORT).show();


                        }


                        if (i == 1)
                        {

                            ClipboardManager cm = (ClipboardManager) mcontext.getSystemService(Context.CLIPBOARD_SERVICE);
                            cm.setText(holder.textViewMessage.getText());
                            Toast.makeText(mcontext, "Copied to clipboard", Toast.LENGTH_SHORT).show();



                        }

                    }
                });


                builder.show();
                return true;
            }
        });
    }else{
        holder.textViewMessage.setBackgroundResource(R.drawable.custom_message_bd_white);
        holder.textViewMessage.setTextIsSelectable(true);
        holder.textViewMessage.setGravity(Gravity.LEFT);
        holder.textViewMessage.setTextColor(Color.BLACK);

        holder.textViewMessage.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {


                CharSequence options[] = new CharSequence[]{"Delete", "Copy Text"};
                final AlertDialog.Builder builder = new AlertDialog.Builder(mcontext);
                builder.setTitle("Select Options");
                builder.setItems(options, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        //Click Event for each item.
                        if (i == 0) {

                            String selectedItem = messages.getMessage();
                            arrayListMessages.remove(selectedItem);
                            adapter.notifyDataSetChanged();

                            Toast.makeText(mcontext, "Message deleted", Toast.LENGTH_SHORT).show();


                        }


                        if (i == 1)
                        {

                            ClipboardManager cm = (ClipboardManager) mcontext.getSystemService(Context.CLIPBOARD_SERVICE);
                            cm.setText(holder.textViewMessage.getText());
                            Toast.makeText(mcontext, "Copied to clipboard", Toast.LENGTH_SHORT).show();



                        }

                    }
                });


                builder.show();
                return true;

            }

        });
    }
    holder.textViewMessage.setText(messages.getMessage());

    if(message_type.equals("text")) {

        holder.textViewMessage.setText(messages.getMessage());
        holder.messageImage.setVisibility(View.INVISIBLE);


    } else {

        holder.textViewMessage.setVisibility(View.INVISIBLE);
        Picasso.with(holder.UserProfile.getContext()).load(messages.getMessage())
                .placeholder(R.drawable.my_profile).into(holder.messageImage);

    }



}

private void copyText() {
}

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

class MyMessageViewHolder extends RecyclerView.ViewHolder{

    TextView textViewMessage;
    CircleImageView UserProfile;
    ImageView messageImage;

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

        textViewMessage = itemView.findViewById(R.id.messageTextView);
        UserProfile = itemView.findViewById(R.id.message_profile_layout);
        messageImage = (ImageView) itemView.findViewById(R.id.message_image_layout);
    }


}
}

关于如何在所选项目上启用删除功能,是否有任何建议?提前谢谢......

1 个答案:

答案 0 :(得分:0)

请注意,您永远不应在setOnLongClickListener方法中使用onBindViewHolder。尝试将其移至MyMessageViewHolder构造函数。

像这样:

public MessageAdapter(Context context, ArrayList<Messages> arrayListMessages){
    this.arrayListMessages = arrayListMessages;
    mcontext = context;
    mAuth = FirebaseAuth.getInstance();
}



class MyMessageViewHolder extends RecyclerView.ViewHolder {

    TextView textViewMessage;
    CircleImageView UserProfile;
    ImageView messageImage;

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

        textViewMessage = itemView.findViewById(R.id.messageTextView);
        UserProfile = itemView.findViewById(R.id.message_profile_layout);
        messageImage = (ImageView) itemView.findViewById(R.id.message_image_layout);

        String mCurrentUser = mAuth.getCurrentUser().getUid();

        final Messages messages = arrayListMessages.get(getLayoutPosition());
        String message_type = messages.getType();
        String from_user = messages.getFrom();

        mUserDatabase =
            FirebaseDatabase.getInstance().getReference().child("Users").child(from_user);

        textViewMessage.setOnLongClickListener(new View.OnLongClickListener() {
            @Override public boolean onLongClick(View view) {

                if (mCurrentUser.equals(messages.getFrom())) {
                    ...
                } else {
                    ...
                }
            }
        }
    }
}