我从另一个教程复制并粘贴了这段代码,并根据自己的需要进行调整。我希望放入表的SQLite id,而不是通过适配器位置。我已经搜索了但是因为我对此很陌生,所以无论我尝试什么都会让我感到困惑。
感谢您的帮助。
公共类RecyclerAdapter扩展了RecyclerView.Adapter {
static List<DatabaseModel> dbList;
static Context context;
RecyclerAdapter(Context context, List<DatabaseModel> dbList ){
this.dbList = new ArrayList<DatabaseModel>();
this.context = context;
this.dbList = dbList;
}
@覆盖 public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.item_row, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerAdapter.ViewHolder holder, int position) {
Integer _id1= dbList.get(position).get_id();
holder.address_h.setText(dbList.get(position).getAddress());
holder.name_h.setText(dbList.get(position).getName());
holder.date_h.setText(dbList.get(position).getSpare1()); // getDatetimedue needed
holder.balance_h.setText("£" + dbList.get(position).getBalance());
holder.address_h.setSelected(holder.address_h.isSelected());
holder.balance_h.setSelected(holder.balance_h.isSelected());
holder.name_h.setSelected(holder.name_h.isSelected());
holder.date_h.setSelected(holder.date_h.isSelected());
}
@Override
public int getItemCount() {
return dbList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView address_h, balance_h, name_h, date_h;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
address_h = (TextView) itemLayoutView.findViewById(R.id.rvname);
balance_h = (TextView)itemLayoutView.findViewById(R.id.rvphone);
name_h = (TextView)itemLayoutView.findViewById(R.id.rvname_h);
date_h = (TextView)itemLayoutView.findViewById(R.id.rvdate_h);
itemLayoutView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(context,DetailsActivity.class);
Bundle extras = new Bundle();
extras.putInt("position",getAdapterPosition());
intent.putExtras(extras);
context.startActivity(intent);
}
}
}