我有一个关于recycleview自定义的适配器,我没有sqlite,也不知道如何使用它
答案 0 :(得分:0)
public class WordAdapter extends RecyclerView.Adapter<WordAdapter.ViewHolder> {
private Context mContext ;
private ArrayList<WordClass> mList;
private OnItemClickListener mListener;
public WordAdapter(Context context, ArrayList<WordClass> liste){
this.mContext = context;
this.mList = liste;
}
public void setOnClickListener(OnItemClickListener listener){
this.mListener = listener;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
View view = LayoutInflater.from(mContext)
.inflate(R.layout.list_item,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
final WordClass wordClass = mList.get(position);
viewHolder.mTextView.setText(wordClass.getmTextV1());
viewHolder.btnCopy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = wordClass.getmTextV1();
ClipboardManager myClipboard = (ClipboardManager) v.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
ClipData myClip = ClipData.newPlainText("label", s);
myClipboard.setPrimaryClip(myClip);
Toast.makeText(v.getContext(),R.string.copy, Toast.LENGTH_SHORT).show();
}
});
viewHolder.btnFav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(),R.string.fav, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return mList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private TextView mTextView;
private ImageButton btnShare;
private ImageButton btnCopy;
private ImageButton btnFav;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mTextView = itemView.findViewById(R.id.text1);
btnShare = itemView.findViewById(R.id.btn_share_text);
btnCopy = itemView.findViewById(R.id.btn_copy_text);
btnFav = itemView.findViewById(R.id.btn_favorite_text);
itemView.setOnClickListener(this);
btnShare.setOnClickListener(this);
btnCopy.setOnClickListener(this);
btnFav.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(mListener != null){
int pos = getAdapterPosition();
mListener.OnItemClick(v,pos);
}
}
}
}