问题描述:

I have the following RecyclerView adapter in place. It's working fine to display the items however I want to pass arguments to a Dialog or Fragment when I click one of the items:

 public class NovaAdapter extends RecyclerView.Adapter<NovaListRowHolder> {

ArrayList<HashMap<String, String>> novaList = new ArrayList<HashMap<String, String>>();

public static final String STATUS = "status";

public static final String NAME = "name";

public static final String ID = "id";

private Context mContext;

public NovaAdapter(Context context, ArrayList<HashMap<String, String>> novaList) {

this.novaList = novaList;

this.mContext = context;

}

@Override

public NovaListRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.instances_list, null);

NovaListRowHolder mh = new NovaListRowHolder(v);

return mh;

}

@Override

public void onBindViewHolder(NovaListRowHolder novaListRowHolder, int i) {

HashMap<String, String> e = novaList.get(i);

novaListRowHolder.name.setText(e.get(NAME));

novaListRowHolder.status.setText(e.get(STATUS));

novaListRowHolder.setId(e.get(ID));

}

@Override

public int getItemCount() {

return (null != novaList ? novaList.size() : 0);

}class NovaListRowHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

protected TextView name;

protected TextView status;

protected String id;

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public NovaListRowHolder(View view) {

super(view);

view.setOnClickListener(this);

this.name = (TextView) view.findViewById(R.id.nameInstance);

this.status = (TextView) view.findViewById(R.id.statusInstance);

}

public void onClick(View view){

Dialog dialog = new Dialog(view.getContext());

dialog.setContentView(R.layout.instances_listdetail);

dialog.setTitle("Details " + name.getText() + " " + getPosition());

dialog.show();

}

At the moment the click is only opening a placeholder dialog but it isn't passing any data. I tried to search everywhere for examples or tutorials on how to do it but didn't find it anywhere.

Sorry in advance it it's too obvious but it's my first app and I don't have much experience.

网友答案:

You should set the onClickListener in onBindViewHolder to each of the views. You can access the itemView directly from your viewHolder. Now you can set a tag (or a few) to the view with the click listener, and retrieve them inside your onClick method.

网友答案:

If you need to pass values from recycler view to dialog means then use this

code of onBindViewHolder

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Offers offers = offersList.get(position);
    holder.topic.setText(offers.getTopic());
    holder.expiry.setText(offers.getExpiry());
}

Simply pass the values of by setText

//TextView inside dialog
 offers_dialog_topic = (TextView) dialog.findViewById(R.id.offers_dialog_offer_type);
 date_year = (TextView) dialog.findViewById(R.id.date_year);

 offers_dialog_topic.setText(topic.getText().toString());
 date_year.setText(expiry.getText().toString());



 Hope this helps for other.
 Thank you.
相关阅读:
Top