如何从getAdapterPosition()获取列表的相应索引?

时间:2016-03-17 10:23:19

标签: android adapter android-recyclerview

这是我的代码。我需要点击视图中的arraylist索引(orderList)。

适配器class.i的完整代码需要orderList的索引而不是布局中的位置

public class OrdersList_Adapter extends

RecyclerView.Adapter {

private static ArrayList<Order> orderList;
private static OnItemClickListener monItemClickListener;
Context context;
int pos;

public OrdersList_Adapter(ArrayList<Order> orderlist, Context context) {
    orderList = orderlist;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.orders_list_item, parent, false);
    OrdersList_Adapter.ViewHolder vh = new ViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    //animate(holder);
   // pos=position;
    int id = orderList.get(position).getId();
    String orderStatus = orderList.get(position).getOrderStatus();
    String pickup_name = orderList.get(position).getName_pickup();

    if (id > 0 && !pickup_name.isEmpty() && !deliver_line1.isEmpty() && !dist_deliver.isEmpty()) {
        holder.txtId.setText(String.valueOf(id));
        holder.txtStatus.setText(orderStatus);

      /*  holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                monItemClickListener.onItemClick(pos);
            }
        });*/
    }

}

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

public class ViewHolder extends RecyclerView.ViewHolder {

    public TextView txtId, txtStatus;

    View v=itemView;
    public ViewHolder(View v) {

        super(v);
        txtId = (TextView) v.findViewById(R.id.tv_orderId);
        txtStatus = (TextView) v.findViewById(R.id.tv_orderStatus);

        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                monItemClickListener.onItemClick(getAdapterPosition());
            }
        });
    }

}

public interface OnItemClickListener {
    void onItemClick(int position);
}

public void setOnItemClickListener(OnItemClickListener listener) {
    this.monItemClickListener = listener;
}

}

1 个答案:

答案 0 :(得分:0)

public void onBindViewHolder(ViewHolder holder, int position) {

    int id = orderList.get(position).getId();
    String orderStatus = orderList.get(position).getOrderStatus();


    if (id > 0) {
        holder.txtId.setText(String.valueOf(id));
        holder.txtStatus.setText(orderStatus);

    }
holder.view.setOnClickListener(new Listener(position));
}




public class ViewHolder extends RecyclerView.ViewHolder {

    public TextView txtId, txtStatus;
View view = v;
    public ViewHolder(View v) {
        super(v);
        txtId = (TextView) v.findViewById(R.id.tv_orderId);
        txtStatus = (TextView) v.findViewById(R.id.tv_orderStatus);
this.view = v;

    }

    class Listener implements View.OnClickListener
    {
    int position;
    Listener(int position)
    {
    this.position = position;
    }


    @Override
            public void onClick(View v) {
            // Here you get the different position

                onItemClickListener.onItemClick(position);//here i get adapter position
            }

    }


}