简体   繁体   中英

RecyclerView onClick get values

I have a sqlite database with columns NAME,LNAME,LNUMBER,VIOLATION,ARRESTPLACE,ADDRESS,PNUMBER,ONAME,DTIME and I have a recyclerview displays the values of LNAME,LNUMBER, and VIOLATION. I have successfully implemented a onclick method on the recyclerview to pass data to another activity but unfortunately I only know how to pass values that are displayed by the recyclerview but what I want to do is also pass NAME,ARRESTPLACE,ADDRESS,PNUMBER,ONAME,DTIME values even if the recyclerview is not displaying them. What should I do to achieve that? This is what my recyclerview looks like.

CLICK HERE

Here are my recycleradapter codes.

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);
    }
}

}

Pass Violator object to intent after implementing Serializable or Parsable Interface to Violator class.

For example:

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

There are several ways to achieve the result: 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. Make your violator class implement Parcelable or Serializable and use the putExtra method like below:

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

让你违反者类Parcelable和发送完整的数组列表为intent.putParcelableArrayListExtra()并得到它的另一端。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM