从RecyclerView项目传递参数

时间:2014-12-01 03:33:32

标签: android onclick arguments android-recyclerview

我有以下RecyclerView适配器。它可以正常显示项目,但是当我点击其中一个项目时,我想将参数传递给Dialog或Fragment:

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

此时点击只打开一个占位符对话框,但它没有传递任何数据。我试图到处寻找有关如何做到但没有在任何地方找到它的示例或教程。

提前抱歉,这太明显了,但这是我的第一个应用,而且我没有多少经验。

2 个答案:

答案 0 :(得分:1)

您应该将onBindViewHolder中的onClickListener设置为每个视图。您可以直接从viewHolder访问itemView。 现在,您可以使用单击侦听器为视图设置标记(或一些标记),并在onClick方法中检索它们。

答案 1 :(得分:0)

如果您需要将值从// instead of : //if(NameOfCard == "Ten") //{ // Console.SetCursorPosition(x + 4, 6); // Console.Write(Ten.CardChar); //} //else //{ // Console.SetCursorPosition(x + 5, 6); // Console.Write(Ace.CardChar); //} // do this : if(_cards.ContainsKey(NameOfCard)) { Console.SetCursorPosition(x + 4, 6); Console.Write(_cards[NameOfCard].CardChar); } else { Console.SetCursorPosition(x + 5, 6); Console.Write(Ace.CardChar); } 传递到recycler view,则请使用此

代码dialog

onBindViewHolder

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

的值即可
setText

希望这有助于其他人。

谢谢。