RecyclerView onClick获取值

时间:2017-09-06 16:25:39

标签: java android android-recyclerview

我有一个带有NAME,LNAME,LNUMBER,VIOLATION,ARRESTPLACE,ADDRESS,PNUMBER,ONAME,DTIME列的sqlite数据库,我有一个recyclerview显示LNAME,LNUMBER和VIOLATION的值。我已经在recyclerview上成功实现了onclick方法,将数据传递给另一个活动但不幸的是我只知道如何传递recyclelerview显示的值,但我想要做的还是传递NAME,ARRESTPLACE,ADDRESS,PNUMBER,ONAME,即使Recyclerview没有显示它们,DTIME值也是如此。我该怎么做才能实现这一目标?这就是我的recyclerview的样子。

CLICK HERE

以下是我的recycleradapter代码。

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder>{

private ArrayList<Violator> arrayList = new ArrayList<>();
Context ctx;


public RecyclerAdapter( ArrayList<Violator> arrayList, Context ctx)
{
    this.arrayList = arrayList;
    this.ctx = ctx;


}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view,parent,false);
    MyViewHolder myViewHolder = new MyViewHolder(view,ctx,arrayList);
    return myViewHolder;
}

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


    holder.LName.setText("Last Name: "+arrayList.get(position).getLName());
    holder.LNumber.setText("License Number: "+arrayList.get(position).getLNumber());
    holder.Violation.setText("Violation: "+arrayList.get(position).getViolation());
    int sync_status = arrayList.get(position).getSync_status();
    if (sync_status == DBContract.SYNC_STATUS_OK)
    {
        holder.Sync_Status.setImageResource(R.drawable.ok);
        holder.Syncstatus.setText("Synced!");
    }
    else
    {
        holder.Sync_Status.setImageResource(R.drawable.sync);
        holder.Syncstatus.setText("Not Synced!");
    }
}

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




public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
    ImageView Sync_Status;
    TextView LName;
    TextView LNumber;
    TextView Violation;
    TextView Syncstatus;

    ArrayList<Violator> arrayList = new ArrayList<Violator>();
    Context ctx;
    public MyViewHolder(View itemView, Context ctx, ArrayList<Violator> arrayList)
    {
        super(itemView);
        this.arrayList = arrayList;
        this.ctx = ctx;
        itemView.setOnClickListener(this);
        Sync_Status = (ImageView)itemView.findViewById(R.id.imgSync);
        LName = (TextView)itemView.findViewById(R.id.txtLname);
        LNumber = (TextView) itemView.findViewById(R.id.txtLnumber);
        Violation = (TextView) itemView.findViewById(R.id.txtViolation);
        Syncstatus = (TextView) itemView.findViewById(R.id.tvSyncStat);
    }

    @Override
    public void onClick(View v) {
        int position = getAdapterPosition();
        Violator violator = this.arrayList.get(position);
        Intent intent = new Intent(this.ctx, Bluetooth_Activity.class);
        intent.putExtra("lastname",violator.getLName());
        this.ctx.startActivity(intent);
    }
}

}

3 个答案:

答案 0 :(得分:0)

在将Vioizable类的Serializable或Parsable接口实现后,将Violator对象传递给intent。

例如:

Intent intent = new Intent(this.ctx, Bluetooth_Activity.class);
intent.putExtra("myViolator",violator);
this.ctx.startActivity(intent);

答案 1 :(得分:0)

有几种方法可以达到结果: 1.

Intent intent = new Intent(this.ctx, Bluetooth_Activity.class);
   //use the putExtra to add more values
   intent.putExtra("lastname",violator.getLName());
   intent.putExtra("violation",violator.getViolation());
   //...and so on
   this.ctx.startActivity(intent);
  1. 让你的violator类实现Parcelable或Serializable,并使用如下的putExtra方法:

    //here I'm using a serializable implementation
    intent.putExtra("my_violator_item", (Serializable) violator);
    

答案 2 :(得分:0)

将您的Violator类设为 Parcelable ,并将完整的数组列表发送为intent.putParcelableArrayListExtra(),然后将其另存为另一端。